简体   繁体   中英

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.

Student POJO class

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

<?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

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

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 {

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.

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".

The tables created in postgresSQL are as follows

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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