`
huakaile
  • 浏览: 62966 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

component、composite、collection映射

阅读更多
1、component(组件)映射:在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid,component可以成为是值对象(DDD)

        采用component映射的好处:它实现了对象模型的细粒度划分,层次会更分明,复用率会更高
//model
public class Contact {
	
	private String email;
	
	private String address;
	
	private String zipCode;
	
	private String contactTel;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getContactTel() {
		return contactTel;
	}

	public void setContactTel(String contactTel) {
		this.contactTel = contactTel;
	}
}



public class User {
	
	private int id;
	
	private String name;
	
	private Contact contact;  // 持有对contact的引用
	
	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 Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}
}
//User.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<component name="contact"><!-- component映射联系类 -->
			<property name="email" length = "10"/>
			<property name="address" length = "10"/>
			<property name="zipCode" length = "10"/>
			<property name="contactTel" length = "10"/>
		</component>
	</class>
</hibernate-mapping>
//test
import org.hibernate.Session;

import junit.framework.TestCase;

public class ComponentMappingTest extends TestCase {

	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			User user = new User();
			user.setName("张三");
			
			Contact contact = new Contact();
			contact.setAddress("xxxxx");
			contact.setEmail("xx@rr.com");
			contact.setZipCode("1111111");
			contact.setContactTel("9999999");
			
			user.setContact(contact); //contact不是实体,是一个附属的类
			
			session.save(user); // update、save的对象为实体
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
}

2、复合(联合)主键映射:通常将复合主键相关的属性,单独放到一个类中
           * 此类必须实现序列化接口
           * 覆写hashcode和equals方法
//model
import java.io.Serializable;

@SuppressWarnings("serial")
public class FiscalYearPeriodPK implements Serializable { // 实现序列化接口

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

	public int getFiscalYear() {
		return fiscalYear;
	}

	public void setFiscalYear(int fiscalYear) {
		this.fiscalYear = fiscalYear;
	}

	public int getFiscalPeriod() {
		return fiscalPeriod;
	}

	public void setFiscalPeriod(int fiscalPeriod) {
		this.fiscalPeriod = fiscalPeriod;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + fiscalPeriod;
		result = prime * result + fiscalYear;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
		if (fiscalPeriod != other.fiscalPeriod)
			return false;
		if (fiscalYear != other.fiscalYear)
			return false;
		return true;
	}
}



import java.util.Date;
  
/**
 * 核算期间
 * @author wangy
 *
 */
public class FiscalYearPeriod {
	
	private FiscalYearPeriodPK fiscalYearPeriodPK;
	
	//开始日期
	private Date beginDate;
	
	//结束日期
	private Date endDate;
	
	//状态
	private String periodSts;

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getPeriodSts() {
		return periodSts;
	}

	public void setPeriodSts(String periodSts) {
		this.periodSts = periodSts;
	}

	public FiscalYearPeriodPK getFiscalYearPeriodPK() {
		return fiscalYearPeriodPK;
	}

	public void setFiscalYearPeriodPK(FiscalYearPeriodPK fiscalYearPeriodPK) {
		this.fiscalYearPeriodPK = fiscalYearPeriodPK;
	}
}
//FiscalYearPeriod.hbm.xml
<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>
//test
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(2008);
			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);
		}
	}			
}

3、set、list、array、map 
//model
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CollectionMapping {
	
	private int id;
	
	private String name;
	
	private Set<String> setValue;
	
	private List<String> listValue;
	
	private String[] arrayValue;
	
	private Map<String, String> mapValue;

	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 String[] getArrayValue() {
		return arrayValue;
	}

	public void setArrayValue(String[] arrayValue) {
		this.arrayValue = arrayValue;
	}

	public void setListValue(List<String> listValue) {
		this.listValue = listValue;
	}

	public List<String> getListValue() {
		return listValue;
	}

	public void setSetValue(Set<String> setValue) {
		this.setValue = setValue;
	}

	public Set<String> getSetValue() {
		return setValue;
	}

	public void setMapValue(Map<String, String> mapValue) {
		this.mapValue = mapValue;
	}

	public Map<String, String> getMapValue() {
		return mapValue;
	}
}
//CollectionMapping.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.CollectionMapping" table="t_CollectionMapping">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="setValue" table="t_set_value"><!-- 映射集合Set,将CollectionMapping普通属性setValue映射成表 -->
			<key column="set_id"/><!-- 增加字段set_id,作为外键指向t_CollectionMapping -->
			<element type="string" column="set_value"/><!-- t_set_value表中保存集合Set的值 -->
		</set>
		<list name="listValue" table="t_list_value"><!-- List集合有顺序 -->
			<key column="list_id"/>
			<list-index column="list_index"/><!-- 加入索引,用于保证顺序 -->
			<element type="string" column="list_value"/>
		</list>
		<array name="arrayValue" table="t_array_value"><!-- 数组有顺序 -->
			<key column="array_id"/>
			<list-index column="array_index"/>
			<element type="string" column="array_value"/>
		</array>
		<map name="mapValue" table="t_map_value">
			<key column="map_id"/>
			<map-key type="string" column="map_key"/><!-- key-value -->
			<element type="string" column="map_value"/>
		</map>
	</class>
</hibernate-mapping>
//test
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

public class CollectionMappintTest extends TestCase {

	public void testSave1() {
		CollectionMapping c = new CollectionMapping();
		
		c.setName("xxx");
		
		Set<String> setValue = new HashSet<String>();
		setValue.add("a");
		setValue.add("b");
		c.setSetValue(setValue);
		
		List<String> listValue = new ArrayList<String>();
		listValue.add("c");
		listValue.add("d");
		c.setListValue(listValue);
		
		String[] arrayValue = new String[]{"e", "f"};
		c.setArrayValue(arrayValue);
		
		Map<String, String> mapValue = new HashMap<String, String>();
		mapValue.put("k1", "v1");
		mapValue.put("k2", "v2");
		c.setMapValue(mapValue);
		
		Session session = null;
		try { // 一般只需要包含会出异常代码
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			session.save(c);
			
			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();
			
			//根据主键加载实体类
			CollectionMapping c = (CollectionMapping)session.load(CollectionMapping.class, 1);
			System.out.println("name=" + c.getName());
			System.out.println("setvalue=" + c.getSetValue());
			System.out.println("mapvalue=" + c.getMapValue());
			System.out.println("listvalue=" + c.getListValue());
			System.out.println("arrayValue=" + c.getArrayValue().length);
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
}
分享到:
评论

相关推荐

    hibernate学习笔记

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

    NHibernate中文帮助手册API

    NHibernate 参考文档手册 chm,NHIBERNATE-... 组件(Component)映射  7.1. 依赖对象(Dependent objects)  7.2. 在集合中出现的依赖对象 (Collections of dependent objects)  7.3. 组件作为IDictionary的索引

    Hibernate3的帮助文档

    7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...

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

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate中文详细学习文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    HibernateAPI中文版.chm

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate3.2中文文档(chm格式)

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    hibernate 框架详解

    9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...

    Hibernate 中文 html 帮助文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    Hibernate+中文文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    最全Hibernate 参考文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联(Ternary associations) 6.3.4. 使用 6.4. 集合例子...

    hibernate3.04中文文档.chm

    7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...

    Hibernate教程

    7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联(Ternary associations) 7.3.4. 使用 7.4. 集合...

    Hibernate3+中文参考文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联(Ternary associations) 6.3.4. 使用 6.4. 集合例子...

    Hibernate3.1_学习源码

    06 06Hibernate_Collection : Hibernate的容器映射技术,包括list、set和map等。用法大体一致,数据库中的两张表,在实体层设计和配置文件都只有一个 其中数据库用到级联删除。配置文件分别用list、set和map元素...

    Hibernate参考文档

    6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...

    NHibernate参考文档 2.0.0 chm

    7. 组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引(Components as IDictionary indices ) 7.4. ...

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

    7. 组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引(Components as IDictionary indices ) 7.4. ...

    hibernate 教程

    映射集合(Mapping a Collection) 6.3. 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. 一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy ...

Global site tag (gtag.js) - Google Analytics