hibernate一对一详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate一对一详解相关的知识,希望对你有一定的参考价值。

IdCard.hbm.xml

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

<hibernate-mapping package="com.hibernate.c_onetoone">
    
    <class name="IdCard" table="t_card">    
    <id name="id"  >
         <generator class="native"/> 
    </id>    
        <property name="number"/>
        <many-to-one name="person" class="Person" column="pserson_id" unique="true"></many-to-one>
    </class>
    
</hibernate-mapping>

1、一对一关系中存在两张表,id card表有外键,所以使用many-to-one 来对应

2、指定class,外键名称column="pserson_id",唯一性unique="true"

 

------------------------------------

Person.hbm.xml

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

<hibernate-mapping package="com.hibernate.c_onetoone">
    
    <class name="Person" table="t_person">    
    <id name="id"  >
         <generator class="native"/> 
    </id>    
        <property name="name"/>
        <one-to-one name="idCard" ></one-to-one>
    </class>
    
</hibernate-mapping>

 1、一对一关系使用one-to-one 因为这张表无外键

---------------------------------------

Person.java

public class Person {
    int id;
    String name;
    IdCard idCard;
    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 IdCard getIdCard() {
        return idCard;
    }
    public void setIdCard(IdCard idCard) {
        this.idCard = idCard;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
    
}

IdCard.java

public class IdCard {
    int id;
    String number;
    Person person;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    @Override
    public String toString() {
        return "IdCard [id=" + id + ", number=" + number + "]";
    }
}

app.java:

public class APP {
    static SessionFactory sessionFactory=null;
    static{
        sessionFactory =new Configuration().configure().addClass(Person.class).addClass(IdCard.class).buildSessionFactory();
    }
    @Test
    public void setApp() {
        Session sess= sessionFactory.openSession();
        Transaction ts=null;
        //
        ts=sess.beginTransaction();
        Person person1=new Person();
        person1.setName("p1");
        IdCard idCard1=new IdCard();
        idCard1.setNumber("000001");
        Person person2=new Person();
        person2.setName("p2");
        IdCard idCard2=new IdCard();
        idCard2.setNumber("000002");
        Person person3=new Person();
        person3.setName("p3");
        IdCard idCard3=new IdCard();
        idCard3.setNumber("000002");
        //
        person1.setIdCard(idCard1);
        person2.setIdCard(idCard2);
        person3.setIdCard(idCard3);
        idCard1.setPerson(person1);
        idCard2.setPerson(person2);
        idCard3.setPerson(person3);
        //
        sess.save(person1);
        sess.save(idCard1);
        sess.save(person2);
        sess.save(idCard2);
        sess.save(person3);
        sess.save(idCard3);
        ts.commit();
        sess.close();
    }

 ------------------------------------

1、一对一关系中会生成两张表,一张有外键,一张无外键

2、有外键的一方用many-to-one ,然后用唯一性进行约束即可

3、无外键一方使用one-to-one 

 

以上是关于hibernate一对一详解的主要内容,如果未能解决你的问题,请参考以下文章

hibernate hibernate一对一关系映射详解

Hibernate学习———— hibernate一对一关系映射详解

hibernate 表关系映射详解之一对一

hibernate 表关系映射详解之一对多

在hibernate内如何配置一对一的关系

hibernate对应关系详解(转)