gpt4 book ai didi

java - 获取引用的属性不是 (One|Many)ToOne Hibernate Exception

转载 作者:行者123 更新时间:2023-11-29 05:13:20 25 4
gpt4 key购买 nike

我是 hibernate 的新手,我在一对一映射中遇到问题。

我有两个 Mysql 表 user 和 userinfo。在 user 表中存储用户的登录凭据,在 userinfo 中存储其信息(如年龄、资格等)。 User 表以 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

如果您能帮助解决此异常,我们将不胜感激。提前致谢。

最佳答案

在类 LICUserInfoModel 中为字段添加注释

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "PK_ID")
private LoginDetailsModel loginDetailsModel;

指定用于关系的列

关于java - 获取引用的属性不是 (One|Many)ToOne Hibernate Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com