Hibernate---->component映射和复合主键映射
一、组件(Component)
组件(Component)是一个被包含的对象,在持久化的过程中,它被当作值类型,而并非一个实体的引用。 如姓名(Name)可以作为人(Person)的一个组件,在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid(object Id),component可以成为是值对象(DDD)
Person.java
public class Person { private java.util.Date birthday; private Name name; private String key; public String getKey() { return key; } private void setKey(String key) { this.key=key; } public java.util.Date getBirthday() { return birthday; } public void setBirthday(java.util.Date birthday) { this.birthday = birthday; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } ...... ...... }
Name.java
public class Name { char initial; String first; String last; public String getFirst() { return first; } void setFirst(String first) { this.first = first; } public String getLast() { return last; } void setLast(String last) { this.last = last; } public char getInitial() { return initial; } void setInitial(char initial) { this.initial = initial; } }
在持久化的过程中,姓名(Name)可以作为人(Person)的一个组件。需要注意的是:你应该为姓名的持久化属性定义getter和setter方法,但是你不需要实现任何的接口或申明标识符字段。
以下是这个例子的Hibernate映射文件:
<class name="eg.Person" table="person"> <id name="Key" column="pid" type="string"> <generator class="uuid"/> </id> <property name="birthday" type="date"/> <component name="Name" class="eg.Name"> <!-- class attribute optional --> <property name="initial"/> <property name="first"/> <property name="last"/> </component> </class>
人员(Person)表中将包括pid, birthday, initial, first和 last等字段。
二、复合主键映射(hibernate强烈不建议使用)
通常将复合主键相关的属性,单独放到一个类中
* 此类必须实现序列化接口
* 覆写hashcode和equals方法(因为主键不能重复)
FiscalYearPeriodPK.java(包含要一起作为主键的元素)
package com.ncepu.hibernate; import java.io.Serializable; 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; } }FiscalYearPeriod.java
package com.ncepu.hibernate; 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; } }
映射文件
<hibernate-mapping> <class name="com.ncepu.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>
加载
public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); FiscalYearPeriodPK pk = new FiscalYearPeriodPK(); pk.setFiscalYear(2008); pk.setFiscalPeriod(8); //这里session.load()中的第一个参数传的是可序列化的id,这里传一个FiscalYearPeriodPK对象. 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); } }