`
dengyll
  • 浏览: 90232 次
社区版块
存档分类
最新评论

单向一对一主键关联实例

阅读更多
IdCard.java类

public class IdCard {

	private int id;
	
	private String cardNo;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
}

Person.java类

public class Person {

	private int id;
	
	private String name;
	
	private IdCard idCard;

	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.hbm.xml映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.hibernate.IdCard" table="t_idCard">
		<id name="id">
			<generator class="native">
			</generator>
		</id>
		<property name="cardNo"/>
	</class>
</hibernate-mapping>

Person.hbm.xml映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.hibernate.Person" table="t_person">
		<id name="id">
			<!-- 采用foreign生成策略,foreign会取得关联对象的标识 -->
			<generator class="foreign">
				<!-- property只关联对象 -->
				<param name="property">idCard</param>
			</generator>
		</id>
		<property name="name"/>
		<!-- one-to-one指示hibernate如何加载其关联对象,默认根据主键加载
			也就是拿到关系字段值,根据对端的主键来加载关联对象
		 -->
		<one-to-one name="idCard" constrained="true"/>
	</class>
</hibernate-mapping>

hibernate.cfg.xml配置文件

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_one2one_pk_1</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		
		<mapping resource="com/hibernate/Person.hbm.xml"/>
		<mapping resource="com/hibernate/IdCard.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

One2OneTest.java测试类(里面用到的工具类前面的博客中已经给出)

       public void testSave1(){
		Session session = null;
		try{
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			IdCard idCard = new IdCard();
			idCard.setCardNo("1111111111111111111");
			
			Person person = new Person();
			person.setName("张三");
			//建立关联
			person.setIdCard(idCard);
			
			//没有抛出TransientObjectException
			//是由一对一关联映射的特性决定的,它必须先保存关联对象IdCard
			//这样它采用foreign映射策略才能取得关联对象的标识
			//也就是它默认了cascade属性
			session.save(person);
			
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}

测试结果(发出的sql语句):

Hibernate: insert into t_idCard (cardNo) values (?)
Hibernate: insert into t_person (name, id) values (?, ?)

分享到:
评论

相关推荐

    Hibernate注释大全收藏

    Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id; } public void setId(Long id) { this.id...一对一 使用 @OneToOne...

    Hibernate3的帮助文档

    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 ...

    hibernate 框架详解

    一对一(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. 多对多...

    hibernate3.04中文文档.chm

    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. 多对多...

    Hibernate教程

    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. 一...

    Hibernate 中文 html 帮助文档

    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参考文档

    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 体系结构与配置 参考文档(html)

    一对一(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 ...

    最全Hibernate 参考文档

    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. 多对多...

    Hibernate3+中文参考文档

    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. 一对一...

    Hibernate使用技巧汇总

    property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名 单向一对多需在一方配置,双向一对多需在双方进行配置 8.lazy=false:被动方的记录由hibernate负责记取,之后存放在主控...

    精通hibernate:对象持久化技术孙卫琴第二版part2

    7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子...

    精通Hibernate:对象持久化技术第二版part3

    7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子...

    jdbc基础和参考

    单向的一对多的关系,在进行关联关系的操作时,会执行不必要的update语句,所以,一般情况下,我们不会做单向一对多的映射。 inverse="true":让其中一方放弃对关联关系的维护 一般在做双向多对一(一对多)关联关系...

    低清版 大型门户网站是这样炼成的.pdf

    4.3.4 映射一对一关联关系 228 4.3.5 映射多对一单向关联关系 235 4.3.6 映射一对多双向关联关系 239 4.3.7 映射一对多双向自身关联关系 244 4.3.8 映射多对多单向关联关系 247 4.3.9 映射多对多双向关联关系 ...

Global site tag (gtag.js) - Google Analytics