我对hibernate很新,我在一对一映射中面临问题。
我有两个Mysql表用户和userinfo。在用户表中存储用户的登录凭证,并且在userinfo中存储其信息(例如年龄,资格等)。用户表将Id作为主键,而用户的PK Id是userinfo表的外键。
以下是我的两个映射类:
@Entity
@Table(name="UserTable")
public class LoginDetailsModel {
@OneToOne(fetch = FetchType.LAZY, mappedBy = "loginDetailsModel", cascade = CascadeType.ALL)
@JoinColumn(name = "id")
private LICUserInfoModel userInfoModel;
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name = "username")
private String username;
@Column(name = "passwd")
private String passwd;
@Column(name = "emailId")
private String emailId;
@Column(name = "userRole")
private String userRole;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the passwd
*/
public String getPasswd() {
return passwd;
}
/**
* @param passwd the passwd to set
*/
public void setPasswd(String passwd) {
this.passwd = passwd;
}
/**
* @return the emailId
*/
public String getEmailId() {
return emailId;
}
/**
* @param emailId the emailId to set
*/
public void setEmailId(String emailId) {
this.emailId = emailId;
}
/**
* @return the userRole
*/
public String getUserRole() {
return userRole;
}
/**
* @param userRole the userRole to set
*/
public void setUserRole(String userRole) {
this.userRole = userRole;
}
/**
* @return the userInfoModel
*/
public LICUserInfoModel getUserInfoModel() {
return userInfoModel;
}
/**
* @param userInfoModel the userInfoModel to set
*/
public void setUserInfoModel(LICUserInfoModel userInfoModel) {
this.userInfoModel = userInfoModel;
}
}
@Entity
@Table(name="UserInfo")
public class LICUserInfoModel {
private LoginDetailsModel loginDetailsModel;
@Id
@Column(name="userinfo_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int userinfo_id;
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "loginDetailsModel"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "ID", unique = true, nullable = false)
private int id;
@Column(name = "fullname")
private String fullname;
@Column(name = "qualification")
private String qualification;
@Column(name = "address")
private String address;
@Column(name = "gender")
private String gender;
@Column(name = "contact_no")
private long contact_no;
@Column(name = "contact_email")
private String contact_email;
@Column(name = "resume")
private String resume;
@Column(name = "dob")
private Date dob;
/**
* @return the userinfo_id
*/
public int getUserinfo_id() {
return userinfo_id;
}
/**
* @param userinfo_id the userinfo_id to set
*/
public void setUserinfo_id(int userinfo_id) {
this.userinfo_id = userinfo_id;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the fullname
*/
public String getFullname() {
return fullname;
}
/**
* @param fullname the fullname to set
*/
public void setFullname(String fullname) {
this.fullname = fullname;
}
/**
* @return the qualification
*/
public String getQualification() {
return qualification;
}
/**
* @param qualification the qualification to set
*/
public void setQualification(String qualification) {
this.qualification = qualification;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* @return the contact_no
*/
public long getContact_no() {
return contact_no;
}
/**
* @param contact_no the contact_no to set
*/
public void setContact_no(long contact_no) {
this.contact_no = contact_no;
}
/**
* @return the contact_email
*/
public String getContact_email() {
return contact_email;
}
/**
* @param contact_email the contact_email to set
*/
public void setContact_email(String contact_email) {
this.contact_email = contact_email;
}
/**
* @return the resume
*/
public String getResume() {
return resume;
}
/**
* @param resume the resume to set
*/
public void setResume(String resume) {
this.resume = resume;
}
/**
* @return the dob
*/
public Date getDob() {
return dob;
}
/**
* @param dob the dob to set
*/
public void setDob(Date dob) {
this.dob = dob;
}
/**
* @return the loginDetailsModel
*/
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public LoginDetailsModel getLoginDetailsModel() {
return loginDetailsModel;
}
/**
* @param loginDetailsModel the loginDetailsModel to set
*/
public void setLoginDetailsModel(LoginDetailsModel loginDetailsModel) {
this.loginDetailsModel = loginDetailsModel;
}
以下是hibernate映射xml:
<beans:bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.lic.agent.LoginDetailsModel</beans:value>
<beans:value>com.lic.agent.LICUserInfoModel</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
当我尝试使用hibernate技术在userinfo表中插入信息时,我得到以下异常:
org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.lic.agent.LICUserInfoModel.loginDetailsModel in mappedBy of com.lic.agent.LoginDetailsModel.userInfoModel
非常感谢您解决此异常的任何帮助。谢谢你提前。
答案 0 :(得分:3)
在您的类LICUserInfoModel中添加字段
的注释@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "PK_ID")
private LoginDetailsModel loginDetailsModel;
指定用于关系的列