1.HibernateTemplate模板类
*装载单个实体的方法,在Spring内部,这些方法都会将具体操作委派给HibernateCallback接口的doInHibernate()回调反复,进而让Session与数据库进行交互。
Object get(...) throws DataAccessException; Object load(....) throws DataAccessException; Object loadAll(....) throws DataAccessException; Object refresh(....) throws DataAccessException; Object contains(....) throws DataAccessException; ....
*存储单个实体的辅助方法
void lock(....) throws DataAccessException; Serializable save(....) throws DataAccessException; void update(....) throws DataAccessException; void saveOrUpdate(....) throws DataAccessException; void persist(....) throws DataAccessException; Object merge(....) throws DataAccessException; void delete(....) throws DataAccessException; void deleteAll(....) throws DataAccessException; void flush(....) throws DataAccessException; void clear(....) throws DataAccessException;
*此外还有基于HQL的查找方法和基于命名查询的查找方法
命名查询方法查找示例如下:
现在test/Query.hbm.xml定义了如下2个命名查询,其中一个还启用了命名参数
<query name="namedVisits"> <![CDATA[ from Visits as vi where vi.id=? ]]> </query> <query name="namedAndParamedVisits"> <![CDATA[ from Visits as vi where vi.id=:id ]]> </query>
接下来再编码中引用
List list1 = this.getHibernateTemplate().findByNamedQuery("namedVisits",2); List list2 = this.getHibernateTemplate().findByNamedQueryAndNamedParam("namedAndParamedVisits",new Stringp[]{"id"},new Object[]{2});
2.关于SessionFactory.getCurrentSession()方法的使用
注意几个细节:
* Session对象是当前活动事务所征集的Session
*DataAccessException异常体系适合于Spring提供的各种DAO层集成技术,比如JDBC,Hibernate,JPA等
@Repository是应用类的注解,其直接应用到DAO对象本身,比如HibernateClinic。
3. LocalSessionFactoryBean
LocalSessionFactoryBean集结了Hibernate SessionFactory相关的所有元数据信息。
如下配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>...../xxx.hbm.xml</value> <value>...../xxx.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.use_sql_comments">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean>
4. 事务管理的支持
需在Spring配置文件中做如下配置
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
5. 基于hibernate的Lob处理
*对Blob字段的操作可详见Spring的imagedb范例。
*Oracle数据库中,较大内容varchar2(4000)往往也很无奈。对此,我们往往采用大对象进行存储(clob),但是由于jdbc驱动对CLOB支持不好,因此使用hibernate进行操作时,往往非常麻烦。但是使用最新的oracle驱动,ojdbc14.jar
然后稍微更改Spring中,SessionFactory的属性参数:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.connection.SetBigStringTryClob">true</prop>
</props>
</property>
<property name="mappingResources">
</property>
</bean>
然后将映射文件中更改成:
<property name="newsContent" type="text">
<column name="NEWS_CONTENT" />
</property>
在POJO类中,直接使用String类型,就可以自由访问了!