Hibernate一对一单向主键XML映射

时间:2014-09-27 17:48:17

标签: java hibernate postgresql mapping one-to-one

我正在尝试在hibernate中创建两个表之间的一对一单向主键关系。我正在使用xml maaping。以下是我的java POJO类及其各自的.hbm文件。

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

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

以下是保存学生和储物柜对象的主要课程。

公共类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();
}

当执行上述java程序时,我收到以下错误。

引起:org.postgresql.util.PSQLException:错误:在表“student”上插入或更新违反外键约束“fk_fcwupt4ogu22gfes87gv8ctp4”   细节:Key(student_id)=(1)不在表“locker”中。

在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)
)

为什么数据没有插入此处。做错了什么有人可以解释一下。

1 个答案:

答案 0 :(得分:0)

我认为映射是正确的。问题在于数据库结构:

请按以下方式更改数据库,然后尝试:

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
)