码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-07-17 20:26:04      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:脚本   hibernate   image   commit   参数   pack   imp   java   数据库表   

这篇讲解 基于外键的双向一对一关联映射

1.考察如下信息,人和身份证之间是一个一对一的关系。表的设计

技术图片

2.类结构

Person.java

public class Person implements Serializable{
    private int id;
    private String name;
    private IdCard idCard;
    public Person() {
    }
    public Person(String name) {
        super();
        this.name = name;
    }

    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;
    }
}

IdCard.java

public class IdCard implements Serializable{
    private String id;
    private String address;
    private Person person;
    public IdCard() {
        // TODO Auto-generated constructor stub
    }
    
    public IdCard(String id, String address) {
        super();
        this.id = id;
        this.address = address;
    }

    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;
    }
}

3.映射文件

Person.hbm.xml

<hibernate-mapping package="cn.sxt.pojo">
    <class name="Person" table="t_person">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name"/>
        <!-- 基于外键的一对一和多对一的表结构是一样的,所以映射文件也一样,要保证外键是唯一的 -->
        <many-to-one name="idCard" column="idCardId" unique="true"></many-to-one>
    </class>
</hibernate-mapping>

IdCard.hbm.xml

<hibernate-mapping package="cn.sxt.pojo">
    <class name="IdCard" table="t_idCard">
        <id name="id">
            <generator class="assigned"></generator>
        </id>
        <property name="address"/>
        <!-- 一对一的双向关联  person表示IdCard类中属性
            property-ref引用的是 person类中IdCard的属性名
         -->
        <one-to-one name="person" property-ref="idCard"></one-to-one>
    </class>
</hibernate-mapping>

4.测试

public class HibernateTest {
    /**
     * 生成数据库表的工具方法
     * */
    @Test
    public void testCreateDB(){
        Configuration cfg = new Configuration().configure();
        SchemaExport se = new SchemaExport(cfg);
        //第一个参数  是否打印sql脚本
        //第二个参数 是否将脚本导出到数据库中执行
        se.create(true, true);
    }
    /**
     * 初始化表数据
     */
    @Test
    public void testInit(){
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            IdCard card1 = new IdCard("110086", "湖北武当");
            IdCard card2 = new IdCard("110087", "光明顶");
            Person p1 = new Person("张三疯");
            p1.setIdCard(card1);
            Person p2 = new Person("殷素素");
            p2.setIdCard(card2);
            session.save(card1);
            session.save(card2);
            session.save(p1);
            session.save(p2);
            tx.commit();
            
        } catch (Exception e) {
            if(tx!=null)
                tx.rollback();
        }finally {
            HibernateUtil.close();
        }
    }
    /**
     */
    @Test
    public void testGetData(){
        Session session = HibernateUtil.getSession();
        Person p1 = (Person)session.get(Person.class, 1);
        System.out.println(p1.getName()+"----"+p1.getIdCard().getId()+"----"+p1.getIdCard().getAddress());
        System.out.println("-------------------------------------------");
        IdCard card = (IdCard)session.get(IdCard.class, "110086");
        System.out.println(card.getId()+"------"+card.getAddress()+"----"+card.getPerson().getName());
        HibernateUtil.close();
    }
}

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

标签:脚本   hibernate   image   commit   参数   pack   imp   java   数据库表   

原文地址:https://www.cnblogs.com/Vincent-yuan/p/11203321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!