简体   繁体   English

休眠一对一单向主键XML映射

[英]Hibernate One to One Unidirectional Primary key XML mapping

I am trying to create one to one unidirectional primary key relation between two tables in hibernate.I am using the xml maaping.Following are my java POJO classes and their respective .hbm files. 我正在尝试在hibernate中的两个表之间创建一对一的单向主键关系。我正在使用xml maaping。以下是我的Java POJO类及其各自的.hbm文件。

Student POJO class 学生POJO班

public class Student {

    private Long studentId;
    private String name;
    private Locker locker;

    public Long getStudentId() {
        return studentId;
    }
    public void setStudentId(Long studentId) {
        this.studentId = studentId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Locker getLocker() {
        return locker;
    }
    public void setLocker(Locker locker) {
        this.locker = locker;
    }
}

Student.hbm.xml Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.avinash.dto.Student" table="STUDENT">
        <id name="studentId" type="long" column="STUDENT_ID">
            <generator class="native"></generator>
        </id>

        <property name="name" type="string">
            <column name="NAME"></column>
        </property> 

        <one-to-one name="locker" class="com.avinash.dto.Locker" cascade="all" constrained="true">
        </one-to-one>

    </class>
</hibernate-mapping>

Locker POJO Class 储物柜POJO类

public class Locker {
    private Long lockerId;
    private String location;
    ...
}

Locker.hbm.xml Locker.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.avinash.dto.Locker" table="LOCKER">
    <id name="lockerId" type="long" column="LOCKER_ID">
        <generator class="native"></generator>
    </id>
    <property name="location" type="string">
        <column name="LOCATION"></column>
    </property>
    </class>
</hibernate-mapping>

Following is my main class to save the student and locker object. 以下是我保存学生和储物柜对象的主要课程。

public class OneToOneUnidirectionalPK { 公共类OneToOneUnidirectionalPK {

public static void main(String[] args) {

    Session session = HibernateUtil.getSessionfactory().openSession();
    session.beginTransaction();

    Locker locker = new Locker();
    locker.setLocation("320, Building 1, First Floor");

    Student student = new Student();
    student.setName("Avinash");
    student.setLocker(locker);

    Serializable id = session.save(student);
    System.out.println("The id is " + id);
    session.getTransaction().commit();
    session.close();
   HibernateUtil.shutdown();
}

When the above java program is executed i get the following error. 当执行上述Java程序时,出现以下错误。

Caused by: org.postgresql.util.PSQLException: ERROR: insert or update on table "student" violates foreign key constraint "fk_fcwupt4ogu22gfes87gv8ctp4" Detail: Key (student_id)=(1) is not present in table "locker". 由以下原因引起:org.postgresql.util.PSQLException:错误:对表“ student”的插入或更新违反了外键约束“ fk_fcwupt4ogu22gfes87gv8ctp4”的详细信息:表(locker)中不存在键(student_id)=(1)。

The tables created in postgresSQL are as follows 在postgresSQL中创建的表如下

CREATE TABLE student
(
  student_id bigint NOT NULL,
  name character varying(255),
  CONSTRAINT student_pkey PRIMARY KEY (student_id),
  CONSTRAINT fk_fcwupt4ogu22gfes87gv8ctp4 FOREIGN KEY (student_id)
      REFERENCES locker (locker_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE locker
(
  locker_id bigint NOT NULL,
  location character varying(255),
  CONSTRAINT locker_pkey PRIMARY KEY (locker_id)
)

Why is the data not being inserted here. 为什么未在此处插入数据。 What is the mistake being done. 做错了什么。 Can someone please explain. 有人可以解释一下。

I think mapping is correct. 我认为映射是正确的。 The problem is with the Database structure: 问题在于数据库结构:

Please change the DB as below and try: 请按如下所示更改数据库,然后尝试:

CREATE TABLE student
(
  student_id bigint NOT NULL,
  name character varying(255),
  **locker bigint NOT NULL,**
  CONSTRAINT student_pkey PRIMARY KEY (student_id),
  CONSTRAINT fk_fcwupt4ogu22gfes87gv8ctp4 FOREIGN KEY **(locker)**
  REFERENCES locker (locker_id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION
)

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

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