hibernate 数据关联一对一 3.2

第一种一对一

person和card,card的id即作为主键,又作为外键 

// 各村对方的一个对象 
public class Person {

    private Integer id;
    private String name;
    private Card card;
}
public class Card {

    private Integer id;
    private String cardnum;
    private Person person;
}

xxx.hbm.xml 的配置

记得在总配置文件中添加这两个配置文件 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kaishengit.pojo">
    
    <class name="Card" table="card">
        <id name="id">
        <!-- 主键生成策略变成foreign,表示又当主键又当外键 -->
            <generator class="foreign">
            <!-- 这个person是属性,指明用card这个类的person属性对应的表的主键作外键 -->
                <param name="property">person</param>
            </generator>
        </id>
        <property name="cardnum"/>
        <one-to-one name="person" class="Person"/>
    </class>


</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kaishengit.pojo">
    
    <class name="Person" table="person">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <!-- cascade="delete"表示删除人的时候把卡删了 -->
        <one-to-one cascade="delete" name="card" class="Card"/>
                
    </class>


</hibernate-mapping>

 

程序执行

        Person p = new Person();
        p.setUsername("tom");
        
        Card c = new Card();
        c.setCardnum("12");
        c.setPerson(p);
        
        session.save(p);
        session.save(c);
/* 这时候有两条sql,都是insert,改变save(p),save(c)的顺序式没用的
因为card的insert必须要有id,id不能为空,因为它作为主键,所以程序只能
先save(p)*/



        Person p = new Person();
        p.setUsername("tom");
        
        Card c = new Card();
        c.setCardnum("12");
        
        p.setCard(c);
        
        session.save(p);
        session.save(c);

/* 这样做就会报错了,因为主键的生成策略是从card找person属性
所以这种情况就只能让又当主键又当外键的那个表来维护关系 */       

查询     这种情况下的查询都是联接查询,有捆绑性

Card card = (Card) session.get(Card.class, 2);
        System.out.println(card.getCardnum());
        System.out.println(card.getPerson().getName());
        
        
        Person p = (Person) session.get(Person.class, 2);
        System.out.println(p.getName());
        System.out.println(p.getCard().getCardnum());

删除

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kaishengit.pojo">
    
    <class name="Person" table="person">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <!-- cascade="delete"表示删除人的时候把卡删了 -->
        <one-to-one cascade="delete" name="card" class="Card"/>
                
    </class>


</hibernate-mapping>

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

第二种一对一,是一对多或者多对一的特殊情况,具有延迟加载的能力

dept和Employee,dept中有eid,Employee中有deptid 

// 各村对方的一个对象
public class Dept {

    private Integer id;
    private String name;
    private Employee employee;
}
public class Employee {

    private Integer id;
    private String name;
    private Dept dept;
}

xxx.hmb.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kaishengit.pojo">
    
    <class name="Dept" table="dept">
        <id name="id">
            <generator class="native"/>
        </id>
        
        <property name="name"/>
        <!-- 是多对一的特殊情况,需要指定unique="true"唯一性 -->
        <many-to-one name="employee" class="Employee" unique="true" column="eid"/>
        
    </class>


</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kaishengit.pojo">
    
    <class name="Employee" table="employee">
        <id name="id">
            <generator class="native"/>
        </id>
        
        <property name="name"/>
        <!-- 是多对一的特殊情况,需要指定unique="true"唯一性 -->
        <many-to-one name="dept" class="Dept" unique="true" column="deptid"/>
        
    </class>


</hibernate-mapping>

程序执行

Employee e = new Employee();
        e.setName("jack");
        
        Dept d = new Dept();
        d.setName("hr");
        
        d.setEmployee(e);
        
        e.setDept(d);
        
        session.save(e);
        session.save(d);

请看这里,这是双向维护关系,而且必须双向维护关系,不可避免的会多一条update语句

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。