`

hibernate Composite复合(联合)主键映射

阅读更多

复合(联合)主键映射

通常将复合主键相关的属性,单独放到一个类中
 * 此类必须实现序列化接口
 * 覆写hashcode和equals方法

 

1、实体

public class FiscalYearPeriod {
	
	private FiscalYearPeriodPK fiscalYearPeriodPK;
	
	//开始日期
	private Date beginDate;
	
	//结束日期
	private Date endDate;
	
	//状态
	private String periodSts;
。。。

 

public class FiscalYearPeriodPK implements Serializable {

	//核算年
	private int fiscalYear;
	
	//核算月
	private int fiscalPeriod; ...

 

 2、映射文件

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.FiscalYearPeriod" table="t_fiscal_year_period">
		<composite-id name="fiscalYearPeriodPK">
			<key-property name="fiscalYear"/>
			<key-property name="fiscalPeriod"/>	
		</composite-id>
		<property name="beginDate"/>
		<property name="endDate"/>
		<property name="periodSts"/>
	</class>
</hibernate-mapping>

 

3、TestCase

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.Session;

import junit.framework.TestCase;

public class CompositeMappingTest extends TestCase {

	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			FiscalYearPeriod fiscalYearPeriod = new FiscalYearPeriod();

			FiscalYearPeriodPK pk = new FiscalYearPeriodPK();
			pk.setFiscalYear(2009);
			pk.setFiscalPeriod(8);
			
			fiscalYearPeriod.setFiscalYearPeriodPK(pk);
			
			fiscalYearPeriod.setBeginDate(new Date());
			fiscalYearPeriod.setEndDate(new Date());
			fiscalYearPeriod.setPeriodSts("Y");
			
			session.save(fiscalYearPeriod);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			FiscalYearPeriodPK pk = new FiscalYearPeriodPK();
			pk.setFiscalYear(2008);
			pk.setFiscalPeriod(8);
			
			FiscalYearPeriod fiscalYearPeriod = (FiscalYearPeriod)session.load(FiscalYearPeriod.class, pk);
			
			System.out.println(fiscalYearPeriod.getPeriodSts());
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}			
}

 

分享到:
评论

相关推荐

    hibernate复合主键映射

    复合主键映射 &lt;br&gt;通常将复合主键相关属性,单独抽取出来,建立一个独立的类 * 必须实现序列化接口 * 必须实现equals和hashcode方法 采用&lt;composite-id&gt;标签进行映射,其它属性采用正常映射

    hibernate 无主键表映射

    hibernate 无主键表映射资源说明: 1. 简单Demo 2. Demo对应的sql语句 3. 对应博客文档

    hibernate学习笔记

    复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对数据库的并发支持 30 悲观锁(hibernate_pessimistic) 30 乐观锁(hibernate_optimistic) 32 HQL...

    hibernate注解案例

    Hibernate 的注解学习 ExportDBAnnotation HibernateAnnotationUtils 【一对一】关系映射【一对多】关系映射 【多对多】关系映射 【composite复合主键】关系映射【component组件】关系映射

    Hibernate3.1_学习源码

    04 04Hibernate_Composite : 复合主键的使用,在开发中很少用到,一般良好的设计都会为一个表添加一个自动增长的主键标识列。其中重点配置方法和Hibernate中普遍采用的方法链编程的使用。还需注意可以将组合主键构建...

    Hibernate中文API大全

    比如,OrderLine类具有一个主键,这个主键依赖于Order的(联合)主键。 &lt;composite-id name="id" class="OrderLineId"&gt; &lt;/composite-id&gt; insert="false" update="false"&gt; .... &lt;/class&gt;

    Hibernate 中文 html 帮助文档

    8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy)...

    hibernate总结

    Java类:材料PO,产品PO,中间PO,中间PO的复合主键类(由于是两个一对多形成的多对多,所以,这里只讲一个一对多,另一个是相同的映射方法) //产品类: public class Product { private Integer cpbh; private ...

    最全Hibernate 参考文档

    8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy)...

    hibernate 体系结构与配置 参考文档(html)

    组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy) ...

    Hibernate教程

    9.4. 组件作为联合标识符(Components as composite identifiers) 9.5. 动态组件 (Dynamic components) 10. 继承映射(Inheritance Mappings) 10.1. 三种策略 10.1.1. 每个类分层结构一张表(Table per class ...

    Hibernate注释大全收藏

    映射主键属性 @Id 注解可将实体Bean中某个属性定义为主键,使用@GenerateValue注解可以定义该标识符的生成策略。 • AUTO - 可以是 identity column, sequence 或者 table 类型,取决于不同底层的数据库 • TABLE ...

    hibernate3.04中文文档.chm

    9.4. 组件作为联合标识符(Components as composite identifiers) 9.5. 动态组件 (Dynamic components) 10. 继承映射(Inheritance Mappings) 10.1. 三种策略 10.1.1. 每个类分层结构一张表(Table per class ...

    Hibernate3的帮助文档

    9.4. 组件作为联合标识符(Components as composite identifiers) 9.5. 动态组件 (Dynamic components) 10. 继承映射(Inheritance Mappings) 10.1. 三种策略 10.1.1. 每个类分层结构一张表(Table per class ...

    Hibernate3+中文参考文档

    8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy)...

    hibernate 框架详解

    组件作为联合标识符(Components as composite identifiers) 9.5. 动态组件 (Dynamic components) 10. 继承映射(Inheritance Mappings) 10.1. 三种策略 10.1.1. 每个类分层结构一张表(Table per class ...

    Hibernate参考文档

    8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy)...

    NHibernate中文帮助手册API

    (联合主键)composite-id  5.1.6. 鉴别器(discriminator)  5.1.7. 版本(version)(可选)  5.1.8. 时间戳 (可选)  5.1.9. 属性  5.1.10. 多对一  5.1.11. 一对一  5.1.12. 组件(component), 动态...

    NHibernate参考文档 2.0.0 chm

    5.1.5. (联合主键)composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. 时间戳 (可选) 5.1.9. 属性 5.1.10. 多对一 5.1.11. 一对一 5.1.12. 组件(component), 动态组件(dynamic-...

    NHibernate中文帮组文档(2008.11月更新)

    5.1.5. (联合主键)composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. 时间戳 (可选) 5.1.9. 属性 5.1.10. 多对一 5.1.11. 一对一 5.1.12. 组件(component), 动态组件(dynamic-...

Global site tag (gtag.js) - Google Analytics