Person.java类
package com.org.model; public class Person { private int id; private String name; private Card card; 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 Card getCard() { return card; } public void setCard(Card card) { this.card = card; } }
Card.java类
package com.org.model; public class Card { private int id; private String cardNum; private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCardNum() { return cardNum; } public void setCardNum(String cardNum) { this.cardNum = cardNum; } }
Person.hbm.xml映射文件
<?xml version="1.0"?> <!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.org.model"> <class name="Person" table="person"> <id name="id"> <generator class="foreign"> <param name="property">card</param> </generator> </id> <property name="name" column="name" not-null="true"/> <!-- constrained="true"表明该类主键由关联类生成 --> <one-to-one name="card" constrained="true"></one-to-one> </class> </hibernate-mapping>
Card.hbm.xml映射文件
<?xml version="1.0"?> <!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.org.model"> <class name="Card" table="card"> <id name="id"> <generator class="native"/> </id> <property name="cardNum" column="card_num" not-null="true"/> <!-- property-ref用于指定关联类的一个属性,这个属性将会和外键相对应,如果没有指定,会使用对方 关联类的主键。 --> <one-to-one name="person" cascade="save-update" property-ref="card"></one-to-one> </class> </hibernate-mapping>
测试类
package com.org.model.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.org.model.Card; import com.org.model.Person; import com.org.util.HibernateUtil; public class HibernateTest1 { @Test public void testSave1() { Session session = null; Transaction tx = null; try { // 拿到session session = HibernateUtil.getSession(); // 开启事务 tx = session.beginTransaction(); // 给实体赋值 Person person = new Person(); person.setName("aaa"); Card card = new Card(); card.setCardNum("11111111111111"); person.setCard(card); card.setPerson(person); session.save(card); //提交事务 tx.commit(); } catch (Exception e) { // 打印堆栈信息 e.printStackTrace(); // 事务回滚 if (tx != null) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } } }
结果:
Hibernate: insert into card (card_num) values (?) Hibernate: insert into person (name, id) values (?, ?)
相关推荐
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向...Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
博文链接:https://shaqiang32.iteye.com/blog/201309
NULL 博文链接:https://paladin1988.iteye.com/blog/1639102
Hibernate学习要点:一对一主键关联双向
博文链接:https://llying.iteye.com/blog/220803
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向...├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
NULL 博文链接:https://lijunabc.iteye.com/blog/438426
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
010 一对一 主键关联映射_双向 011 一对一 唯一外键关联映射_单向 012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多关联映射 单向 017 多对多关联...
NULL 博文链接:https://cdxs2.iteye.com/blog/1932867
hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person---->IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 11 session ...
Hibernate学习要点:一对一主键关联双向
一对一关联可能是双向的,在双向关联中,有且仅有一端作为主体(owner)端存在:主体端负责维护联接列(即更新),对于不需要维护这种关系的从表则通过mappedNy属性进行声明。mappedBy的值指向主体的关联属性。例子...
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚
自己记录的学习笔记,内容很详细,有代码,包括:一对多双向关联关系、一对多双向自身关联关系、一对一关联关系(主键关联、外键关联)、一对多与一对一结合、多对多关联关系、Map、Set、List与Bag映射关系、查询...
Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...
7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...
8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一...
一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...