类图如下:
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="native"/> </id> <property name="name" column="name" not-null="true"/> <!-- name指Student类中对应一方的属性名 cascade属性指定级联操作 unique指定外键的唯一性 class指一方的类名 column表示关联的外键 not-null表示此外键不能为空 --> <many-to-one name="card" class="Card" cascade="save-update" unique="true" column="card_id" not-null="true"> </many-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, card_id) values (?, ?)
相关推荐
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. 多对多...
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.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...
7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...
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. 多对多...
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 ...
7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...
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.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ...
7.1.2 一对一的外键关联 7.1.3 用联结表映射 7.2 多值的实体关联 7.2.1 一对多关联 7.2.2 多对多关联 7.2.3 把列添加到联结表 7.2.4 映射map 7.3 多态关联 7.3.1 多态的多对一...
8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...
一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...
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. 多对多...
7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向关联(Bidirectional associations) 7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的...
property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名 单向一对多需在一方配置,双向一对多需在双方进行配置 8.lazy=false:被动方的记录由hibernate负责记取,之后存放在主控...
基于外键的一对一 Wife Husband id id name name h_id references Husband(id) unique 基于主键的一对一 Wife Husband id references Husband(id) id name name create table Husband( id number ...