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