简体   繁体   English

休眠外键一对一null

[英]Hibernate one-to-one null in foreign key

Am trying to do "one to one" mapping with hibernate entities. 我正在尝试与休眠实体进行“一对一”映射。 student table has contact_info as a foreign key reference. 学生表具有contact_info作为外键引用。 When i save the student object, foreign key sets as NULL. 当我保存学生对象时,外键设置为NULL。

here i have attached sources. 我在这里附上了消息来源。 pls let me know the problem... 请让我知道问题所在...

Tables : 桌子:

DROP TABLE IF EXISTS cont_info;
CREATE TABLE cont_info(
ID INT PRIMARY KEY AUTO_INCREMENT,
MOBILE_NO VARCHAR(40),
EMAIL_ID VARCHAR(40)
);

DROP TABLE IF EXISTS student;
CREATE TABLE student(
ID INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(40),
CONTACT_INFO INT,
KEY `CONTACT_INFO` (`CONTACT_INFO`),
CONSTRAINT `fk_student_contact_info` FOREIGN KEY (`CONTACT_INFO`) REFERENCES `cont_info` (`ID`) 
);

Entities : 实体:

ContactInfo.java ContactInfo.java

import java.io.Serializable;

public class ContactInfo implements Serializable {

    private int id;
    private String mobileNo;
    private String emailId;
    private Student student;

    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getMobileNo() {
        return mobileNo;
    }
    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }
    public String getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

}

Student.java : Student.java:

import java.io.Serializable;

public class Student implements Serializable {
    private int id;
    private String name;
    private ContactInfo contactInfo;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ContactInfo getContactInfo() {
        return contactInfo;
    }
    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
}

HBM : HBM:

<class name="com.tutorial.hibernate.ContactInfo" table="cont_info">
    <id column="id" name="id">
        <generator class="identity" />          
    </id>
    <one-to-one name="student" class="com.tutorial.hibernate.Student" cascade="save-update"></one-to-one>
    <property name="mobileNo" column="MOBILE_NO" />
    <property name="emailId" column="EMAIL_ID" />   
</class>

<class name="com.tutorial.hibernate.Student" table="student">
    <id name="id" column="id">          
        <generator class="foreign">
            <param name="property">contactInfo</param>
        </generator>
    </id>
    <one-to-one name="contactInfo" class="com.tutorial.hibernate.ContactInfo" constrained="true"></one-to-one>
    <property name="name" column="NAME" />      
</class>

Java Code to save the entity : Java代码保存实体:

Session session = HibernateUtil.getSession();
Transaction txn = session.beginTransaction();

ContactInfo contactInfo = new ContactInfo();
contactInfo.setMobileNo("9876543212");
contactInfo.setEmailId("test@gmail.com");

Student student = new Student();
student.setName("Student1");
contactInfo.setStudent(student);

student.setContactInfo(contactInfo);
session.save(student);

txn.commit();
session.close();

when executing above line i can see sql like. 当执行以上行时,我可以看到sql之类的。

Hibernate: insert into cont_info (MOBILE_NO, EMAIL_ID) values (?, ?)
Hibernate: insert into student (NAME, id) values (?, ?)

but actually the table shows. 但实际上表格显示了。

contact_info在此处输入图片说明

This is causing problem in mapping file of your code 这导致代码映射文件出现问题

generator class="foreign"

Try to use this student mapping file as same as contact_info mapping file 尝试将此student映射文件与contact_info映射文件相同

<id column="id" name="id">
        <generator class="identity" />          
    </id>

EDIT in student mapping file need to mention column name 学生映射文件中的EDIT需要提及列名

<one-to-one name="contactInfo" class="com.tutorial.hibernate.ContactInfo" constrained="true" column="CONTACT_INFO"></one-to-one>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM