hibernate 之 关联映射的基于外键的双向一对一关联
Posted forever_2h
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate 之 关联映射的基于外键的双向一对一关联相关的知识,希望对你有一定的参考价值。
1. 人和身份证是一个一对一的关系,他们的表结构为:
2. 类结构:Person.java
public class Person { private int id; private String name; private int age; private IdCard idCard; public Person() { } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } }
IdCard.java
public class IdCard { private String id; private String address; private Person person; public IdCard() { } 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> <class name="cn.wh.vo.Person" table="t_person"> <id name="id"> <generator class="native"></generator> </id> <property name="name"/> <property name="age"/> <!-- 配置基于外键的一对一的关联映射 和多对一映射几乎一致,需要设置unique=true --> <many-to-one name="idCard" column="idCardId" unique="true"/> </class> </hibernate-mapping>
IdCard.hbm.xml
<hibernate-mapping> <class name="cn.wh.vo.IdCard" table="t_idCard"> <id name="id"> <generator class="assigned"></generator> </id> <property name="address"/> <one-to-one name="person" property-ref="idCard"/> </class> </hibernate-mapping>
4. 测试:
public class HibernateTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); //第一个参数 是否打印 数据库脚本 //第二个参数 是否将脚本导入数据库执行 export.create(true, true); } @Test public void testInit(){ Transaction tx = null; try { tx = session.beginTransaction(); IdCard card1 = new IdCard("1101","北京昌平"); IdCard card2 = new IdCard("1102", "北京海淀"); Person p1 = new Person("张三",22); p1.setIdCard(card1); Person p2 = new Person("李四",23); 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(); } } @Test public void testGet(){ Person p1 = (Person)session.get(Person.class, 1); System.out.println(p1.getName()+"---"+p1.getAge()+"----"+p1.getIdCard().getAddress()); System.out.println("----------------------------"); IdCard card1 = (IdCard)session.get(IdCard.class, "1101"); System.out.println(card1.getId()+"---"+card1.getAddress()+"---"+card1.getPerson().getName()); } }
以上是关于hibernate 之 关联映射的基于外键的双向一对一关联的主要内容,如果未能解决你的问题,请参考以下文章