2011-08-25 114 views
3
enter image description here

冬眠與化合物鍵OneToOne關係

@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) 
+0

請發佈您的代碼,包括Hibernate映射和導入語句。 – EkcenierK

+0

我已經發布代碼 – jalil

回答

0

添加user_iduser_name getter方法在你User類,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; 

最後,不要忘了一個getter和setter添加到User爲新領域。

+0

另一個異常現在我已編輯例外 – jalil

+0

是它在那裏的舊地方 – jalil

+0

請不要編輯舊的例外,因爲它不再適用於舊的代碼。 – EkcenierK