Mais conteúdo relacionado
Semelhante a Hibernate教程 (20)
Mais de Shilong Sang (16)
Hibernate教程
- 1. 精通 Hibernate java 对象持久化技术祥解 Hibernate 入门 OR 映射技术 通过 Hibernate API 操纵数据库 检索策略和方式 数据库事务、并发、缓存与性能优化 高级配置。
- 62. 第五章 映射对象标识符 适用于自然主键。由 java 程序负责生成标识符。不能把 setID() 方法声明为 Private 的。尽量避免使用自然主键。 适用于代理主键。 Hibernate 采用 128 位的 UUID 算法来生成标识符。该算法 能够在网络环境中生成唯一的字符串标识符,这种策略并不流行,因为字符串类型的主键比整数类型的主键占用更多的数据库空间。 适用于代理主键。根据底层数据库对自动生成表示符的能力来选择 identity 、 sequence 、 hilo 适用于代理主键。 Hibernate 根据 hign/low 算法生成标识符。 Hibernate 把特定表的字段作为“ hign” 值。默认情况下,采用 hibernate_unique_key 表的 next_hi 字段。 适用于代理主键。 Hibernate 根据底层数据库序列生成标识符。条件是数据库支持序列。 适用于代理主键。由底层数据库生成表识符。条件是数据库支持自动增长数据类型。 适用于代理主键。由 hibernate 自动以递增的方式生成表识符,每次增量为 1 描述 Increment Identity Sequence Hilo Uuid.hex Native assigned 表示符生成器
- 70. 第六章 映射一对多关联关系 Order 到 Customer 的多对一单向关联 Customer 到 Order 的一对多单向关联 Customer 到 Order 的一对多双向关联
- 73. 第六章 映射一对多关联关系 tx = session.beginTransaction(); Customer c = new Customer(); c.setName("TOM"); session.save(c); ? 会有什么后果 Order o1 = new Order(); o1.setOrderNumber("1"); o1.setCustomer(c); Order o2 = new Order(); o2.setOrderNumber("1"); o2.setCustomer(c); session.save(o1); session.save(o2); tx.commit();
- 94. 第七章 操纵持久化对象 结束生命周期 结束生命周期 c2 = null; 处于游离态 转变为游离态 处于持久化状态 转变为持久化状态 临时状态 状态 处于生命周期中 处于生命周期中 处于生命周期中 处于生命周期中 开始生命周期 生命周期 tx = session.beginTransaction(); Customer c = new Customer(“Tom”,new HashSet); Session.save(c) Long id=c.getId(); c = null; Customer c2 = (Customer)session.load(Customer.class,id); tx.commit(); session.close(); c2,getName(); 程序代码
- 108. public class AuditLogInterceptor implements Interceptor, Serializable { private Session session; private Set inserts=new HashSet(); private Set updates=new HashSet(); public void setSession(Session session){ this.session=session; } public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types){ if ( entity instanceof Auditable ) { updates.add(entity); } return false; } public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { return false; } public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) { if ( entity instanceof Auditable ) { inserts.add(entity); } return false; } public void postFlush(Iterator entities) { try{ Iterator it=updates.iterator(); while(it.hasNext()){ Auditable entity=(Auditable)it.next(); AuditLog.logEvent("update",entity,session); } it=inserts.iterator(); while(it.hasNext()){ Auditable entity=(Auditable)it.next(); AuditLog.logEvent("insert",entity,session); } }catch(Exception e){e.printStackTrace();} finally{ inserts.clear(); updates.clear(); } } }
- 111. 第八章 映射组成关系 Class customer{ …… Address homeAddress ; Address comAddress ; …… } Table customers{ …… home_province home_city home_street com_province com_city com_street …… } <compont name=“homeaddress” class=“mypack.Address”> <parent name=“customer” /> <property name=“street” type=“string” column=“HOME_STREET” /> <property name=“city” type = “string” cloumn=“HOME_CITY” /> <property name=“street” type = “string” cloumn=“HOME_STREET” /> </component> <compont name=“comaddress” class=“mypack.Address”> <parent name=“customer” /> <property name=“street” type=“string” column=“COM_STREET” /> <property name=“city” type = “string” cloumn=“COM_CITY” /> <property name=“street” type = “string” cloumn=“COM_STREET” /> </component>
- 128. <property name="name" type="mypack.NameCompositeUserType" > <column name="FIRSTNAME" length="15" /> <column name="LASTNAME" length="15" /> </property> <property name="homeAddress" type="mypack.AddressUserType" > <column name="HOME_STREET" length="15" /> <column name="HOME_CITY" length="15" /> <column name="HOME_PROVINCE" length="15" /> <column name="HOME_ZIPCODE" length="6" /> </property> <property name="comAddress" type="mypack.AddressUserType" > <column name="COM_STREET" length="15" /> <column name="COM_CITY" length="15" /> <column name="COM_PROVINCE" length="15" /> <column name="COM_ZIPCODE" length="6" /> </property>
- 170. 影射继承关系 company 类 SE 类 HE 类 Employee 类 company.hbm.xml SE.hbm.xml HE.hbm.xml company 表 SE 表 HE 表
- 173. 影射继承关系 <class name=“employee” table=“employee”> <id ...></id> <discriminator column=“EMPLOYEE_TYPE” type=“string”/> ... <subclass name=“HE” discriminator-value=“HE”> <propery name=“rate” column=“RATE” type=“double”/> </subclass> <subclass name=“SE” discriminator-value=“SE”> <propery name=“salary” column=“SALARY” type=“double”/> </subclass> </class>