Search code examples
javamysqlhibernate

Getting Referenced property not a (One|Many)ToOne Hibernate Exception


I am very new to hibernate and I am facing problem in one to one mapping.

I have two Mysql table user and userinfo. In user table user's login credentials are stored and in userinfo its information(such as age, qualification etc.) is stored. User table has Id as a primary key whereas users's PK Id is foreign key of userinfo table.

Following are my two mapping classes:

@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;
}

And following is hibernate mapping 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>

When I try to insert information in userinfo table using hibernate technology I get following Exception :

org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.lic.agent.LICUserInfoModel.loginDetailsModel in mappedBy of com.lic.agent.LoginDetailsModel.userInfoModel

Any help to solve this exception would be much appreciated.


Solution

  • In you class LICUserInfoModel add annotation for the field

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

    to specify which column to use for the relation