@Embeddable
public class UserCompoundKey implements Serializable {

    private int user_id;
    private String user_name;

    public UserCompoundKey(){

    }
    public UserCompoundKey(int userid,String username){
        this.user_id = userid;
        this.user_name= username;
    }
    public UserCompoundKey(String username){
        this.user_name= username;
    }

    //@Column(columnDefinition="int(255) NOT NULL AUTO_INCREMENT")
    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }
    //@Column(nullable=false,columnDefinition="varchar(255) default 'no name'")
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }


}

用户类别
@Entity
@Table(name="user")
@IdClass(UserCompoundKey.class)
public class User {



    private int user_id;
    private String user_name;
    private String user_pass;
    private int company_id;
    private int user_type_id;
    private boolean is_expire;
    private Date last_login_date;
    private boolean login_status;
    private UserDetail userDetail;

    public User(){}
    public User(UserCompoundKey userCompoundKey){
        this.user_id = userCompoundKey.getUser_id();
        this.user_name = userCompoundKey.getUser_name();
    }

    @Id
    @AttributeOverrides({
    @AttributeOverride(name = "user_name",
    column = @Column(columnDefinition="varchar(255) default 'no name'")),
    @AttributeOverride(name = "user_id",
    column = @Column(columnDefinition="int(255) NOT NULL AUTO_INCREMENT"))
    })




    @Column(name="user_password")
    public String getUser_pass() {
        return user_pass;
    }
    public void setUser_pass(String user_pass) {
        this.user_pass = user_pass;
    }
    public int getCompany_id() {
        return company_id;
    }
    public void setCompany_id(int company_id) {
        this.company_id = company_id;
    }
    public int getUser_type_id() {
        return user_type_id;
    }
    public void setUser_type_id(int user_type_id) {
        this.user_type_id = user_type_id;
    }
    public boolean isIs_expire() {
        return is_expire;
    }
    public void setIs_expire(boolean is_expire) {
        this.is_expire = is_expire;
    }
    public Date getLast_login_date() {
        return last_login_date;
    }
    public void setLast_login_date(Date last_login_date) {
        this.last_login_date = last_login_date;
    }
    public boolean isLogin_status() {
        return login_status;
    }
    public void setLogin_status(boolean login_status) {
        this.login_status = login_status;
    }
    public void setUserDetail(UserDetail userDetail) {
        this.userDetail = userDetail;
    }
    @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinTable(name = "user_info",
    joinColumns = { @JoinColumn(name = "user_id") },
    inverseJoinColumns = {@JoinColumn(name = "user_id") }
    )
    public UserDetail getUserDetail() {
        return userDetail;
    }



}

用户详细信息
@Entity
@Table(name="user_info")
public class UserDetail {


    private int user_id;
    private String first_name;
    private String last_name;
    private String email;
    private String phone_number;
    private String address_1;
    private String address_2;
    private String country;
    private String city;
    private String state;
    private Date created_date;


    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }
    @Id
    @GeneratedValue
    public int getUser_id() {
        return user_id;
    }
    public String getFirst_name() {
        return first_name;
    }
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone_number() {
        return phone_number;
    }
    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }
    public String getAddress_1() {
        return address_1;
    }
    public void setAddress_1(String address_1) {
        this.address_1 = address_1;
    }
    public String getAddress_2() {
        return address_2;
    }
    public void setAddress_2(String address_2) {
        this.address_2 = address_2;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public Date getCreated_date() {
        return created_date;
    }
    public void setCreated_date(Date created_date) {
        this.created_date = created_date;
    }


}

显示此错误

第一个例外
 Unable to find properties (user_name, user_id) in entity annotated with @IdClass:com.wellclub.test.User
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:774)
    at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at com.wellclub.test.UserTest.main(UserTest.java:17)

第二次例外
A Foreign key refering com.wellclub.test.User from com.wellclub.test.User has the wrong number of column. should be 2
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:273)
    at org.hibernate.cfg.annotations.EntityBinder.bindJoinToPersistentClass(EntityBinder.java:520)
    at org.hibernate.cfg.annotations.EntityBinder.createPrimaryColumnsToSecondaryTable(EntityBinder.java:510)
    at org.hibernate.cfg.annotations.EntityBinder.finalSecondaryTableBinding(EntityBinder.java:441)
    at org.hibernate.cfg.SecondaryTableSecondPass.doSecondPass(SecondaryTableSecondPass.java:25)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:325)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at com.wellclub.test.UserTest.main(UserTest.java:17)

最佳答案

user_id类中为user_nameUser添加getter方法,Hibernate需要这些方法才能访问它们。

public int getUser_id() {
  return user_id;
}

public String getUser_name() {
  return user_name;
}

除上述以外:

映射组合键有几种不同的策略,看起来您正在混合它们。

由于您已经定义了复合键类UserCompoundKey并将其标记为@Embeddable

您不需要类注释:
@IdClass(UserCompoundKey.class)

@Id
@AttributeOverrides({
@AttributeOverride(name = "user_name",
column = @Column(columnDefinition="varchar(255) default 'no name'")),
@AttributeOverride(name = "user_id",
column = @Column(columnDefinition="int(255) NOT NULL AUTO_INCREMENT"))
})

User中删除这些注释。

您也不需要以下字段:
private int user_id;
private String user_name;

User中,因为这些字段已经包含在UserCompoundKey类中。也将其删除。

然后,您只需要在“用户”中添加一个新字段即可映射您已经定义的复合键类键:
@Id
private UserCompoundKey compoundId;

最后,不要忘记在User的新字段中添加一个getter和setter。

关于java - 使用复合键 hibernate OneToOne关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7187381/

10-10 13:13