0

我正在尝试与休眠实体进行“一对一”映射。学生表有contact_info 作为外键引用。当我保存学生对象时,外键设置为 NULL。

在这里我附上了来源。请让我知道问题所在...

表:

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`) 
);

实体:

联系信息.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;
    }

}

学生.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 :

<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 代码:

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();

执行上述行时,我可以看到类似的 sql。

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

但实际上表格显示。

联系方式 在此处输入图像描述

4

1 回答 1

0

这导致代码的映射文件出现问题

generator class="foreign"

尝试将此映射文件与映射student文件一样使用contact_info

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

在学生映射文件中编辑需要提及列名

<one-to-one name="contactInfo" class="com.tutorial.hibernate.ContactInfo" constrained="true" column="CONTACT_INFO"></one-to-one>
于 2013-03-04T07:11:43.663 回答