1、component(组件)映射:在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid,component可以成为是值对象(DDD)
采用component映射的好处:它实现了对象模型的细粒度划分,层次会更分明,复用率会更高
2、复合(联合)主键映射:通常将复合主键相关的属性,单独放到一个类中
* 此类必须实现序列化接口
* 覆写hashcode和equals方法
3、set、list、array、map
采用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); } } }
发表评论
-
JAVA手动连接数据库
2011-04-17 10:02 855// jsp <%@ page language=&q ... -
hibernate CoreAPI
2011-04-14 09:22 656// model import java.util.Date ... -
抓取策略
2011-03-31 09:41 6611、hibernate抓取策略(单端代理的批量抓取)(抓取当前 ... -
缓存(查询优化)
2011-03-31 08:57 6291)hibernate一级缓存:一级缓存很短和session的 ... -
hibernate查询语言(hql)
2011-03-30 08:57 628在hql中关键字不区分大 ... -
读取文件夹生成目录树
2011-03-30 08:18 1374// model import java.util.Set; ... -
悲观锁、乐观锁
2011-03-28 09:05 6321、悲观锁(处理并行问题):悲观锁的实现,通常依赖于数据库机制 ... -
继承映射
2011-03-27 14:21 658每棵继承树映射成一张表 1、理解如何映射 ... -
lazy策略
2011-03-27 13:56 555hibernate lazy策略可以使用在: ... -
多对多关联映射
2011-03-26 11:05 592hibernate多对多关联映射(单向User----> ... -
一对多关联映射
2011-03-26 10:55 697hihernate一对多关联映射(单向Classes----- ... -
session_flush
2011-03-26 10:46 700session flush测试: session flush ... -
一对一唯一外键关联映射
2011-03-26 10:37 639hibernate一对一唯一外键关联映射(单向关联Person ... -
一对一主键关联映射
2011-03-26 10:02 565hibernate一对一主键关联映射(单向关联Person-- ... -
多对一关联映射
2011-03-26 09:31 617hibernate多对一关联映射 关联映射的本质: * 将 ... -
基本映射
2011-03-26 09:14 589hibernate基本映射 实体类---表 实体类中的普通属 ... -
Hibernate基础
2011-03-24 08:34 781import org.hibernate.cfg.Config ... -
get、load加载
2011-03-24 08:30 455junit简介: ... -
HQL(对象查询语言) 和 SQL(结构化查询语言)
2011-02-18 08:44 11211. 查询整个映射对象所有字段 //返回Student对象的 ... -
java基础
2011-02-17 09:09 440使用equals函数 str.e ...
相关推荐
组件component映射(hibernate_component) 27 复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对数据库的并发支持 30 悲观锁(hibernate_pessimistic...
NHibernate 参考文档手册 chm,NHIBERNATE-... 组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引
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. 集合...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
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. 集合例子...
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. 集合...
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. 集合...
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. 集合例子...
06 06Hibernate_Collection : Hibernate的容器映射技术,包括list、set和map等。用法大体一致,数据库中的两张表,在实体层设计和配置文件都只有一个 其中数据库用到级联删除。配置文件分别用list、set和map元素...
6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 三重关联(Ternary ...
7. 组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引(Components as IDictionary indices ) 7.4. ...
7. 组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引(Components as IDictionary indices ) 7.4. ...
映射集合(Mapping a Collection) 6.3. 值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. 一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy ...