java之hibernate之基于外键的双向一对一关联映射
Posted vincent-yuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之hibernate之基于外键的双向一对一关联映射相关的知识,希望对你有一定的参考价值。
这篇讲解 基于外键的双向一对一关联映射
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之基于外键的双向一对一关联映射的主要内容,如果未能解决你的问题,请参考以下文章