hibernate基础10:关联映射之基于主键的双向一对一

Posted 来临

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate基础10:关联映射之基于主键的双向一对一相关的知识,希望对你有一定的参考价值。

Java实体bean类

package com.project.pojo;

import java.io.Serializable;

public class Card implements Serializable{
    private String id;
    private String address;
    private Person person;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }

}
package com.project.pojo;

import java.io.Serializable;

public class Person implements Serializable{
    private String id ;
    private String name;
    private String sex;
    private Card card;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Card getCard() {
        return card;
    }
    public void setCard(Card card) {
        this.card = card;
    }

}

2、配置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.project.pojo">
    <class name="Card" table="t_card">
        <id name="id">
            <generator class="assigned" />
        </id>
        <property name="address" />
        <one-to-one name="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.project.pojo">
    <class name="Person" table="t_person">
        <id name="id">
            <generator class="foreign">
                <param name="property">card</param>
            </generator>
        </id>
        <property name="name" />
        <property name="sex" />
        
        <!-- 基于主键的一对一  constrained为true时将添加外键约束 -->
        <one-to-one name="card" constrained="true"></one-to-one>
    </class>

</hibernate-mapping>

3、测试

package com.project.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.project.pojo.Card;
import com.project.pojo.Person;
import com.project.util.HibernateUtil;

public class HibernateTest {
    Session session = null;
    Transaction ts = null;
    
    @Before
    public void setUp(){
        session = HibernateUtil.getSession();
        ts = session.beginTransaction();
    }
    
    @After
    public void tearDown(){
        HibernateUtil.closeSession();
    }
    
    @Test
    public void testCreateDB(){
        Configuration cfg = new Configuration().configure();
        //使得hibernate映射信息转换为数据库识别的dll语言
        SchemaExport se = new SchemaExport(cfg);
        //第一个参数:是否打印dll语句
        //第二个参数:是否将dll到数据库中执行
        se.create(true, true);
    }
    
    @Test
    public void testInit(){
        try {
            Card c1 = new Card();
            c1.setId("1001");
            c1.setAddress("xx市1区");
            Card c2 = new Card();
            c2.setId("1002");
            c2.setAddress("xx市2区");
            session.save(c1);
            session.save(c2);
            
            Person p1 = new Person();
            p1.setName("张三");
            p1.setSex("男");
            p1.setCard(c1);
            Person p2 = new Person();
            p2.setName("李四");
            p2.setSex("男");
            p2.setCard(c2);
            
            session.save(p1);
            session.save(p2);
            ts.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if(ts!=null)ts.rollback();
        }
    }
    
    @Test
    public void testSelect(){
        Person p = (Person)session.get(Person.class, "1001");
        System.out.println(p.getName()+"	"+p.getSex());
        System.out.println("---------------");
        System.out.println(p.getCard().getAddress());
        System.out.println("=================");
        Card c = (Card)session.get(Card.class, "1001");
        System.out.println(c.getAddress());
        System.out.println("---------------");
        System.out.println(c.getPerson().getName()+"	"+c.getPerson().getSex());
        
        
    }
}

 

以上是关于hibernate基础10:关联映射之基于主键的双向一对一的主要内容,如果未能解决你的问题,请参考以下文章

hibernate基础10:关联映射之基于主键的双向一对一

hibernate基础10:关联映射之基于主键的双向一对一

hibernate基础08:关联映射之基于外键的双向一对一

Hibernate关联关系映射之一对一关联关系

hibernate中基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同

java之hibernate之基于外键的双向一对一关联映射