Spring Study[13]

Managing Hibernate resources:
As we said, you will keep a single instance of a SessionFactory throughout the life of your application.
<bean id="sessionFactory"class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

The preferred way to do this is to wire a DataSource to the LocalSessionFactoryBean:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/trainingDatasource</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>

with Spring you do not have to manage these configurations in a separate properties file. Instead, you can wire them to the hibernateProperties property of the LocalSessionFactoryBean:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
</props>
</property>

</bean>

One last thing you must configure is which mapping files Hibernate should read in.
In this case, we use the mapping- Resources property:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>Student.hbm.xml</value>
<value>Course.hbm.xml</value>

</list>
</property>

</bean>

You can also configure the mappingDirectoryLocations property with a path that is a subset of your application’s
class path, and Spring will configure the SessionFactory with every *.hbm.xml it finds in this path.
<bean id="sessionFactory" class="org.springframework.
orm.hibernate.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/springinaction/training/model</value>
</list>
</property>

</bean>

Like all of Spring’s DAO frameworks, this will be a template class. In this case, it is the HibernateTemplate class.

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
<property name="sessionFactory">
 <ref bean="sessionFactory"/>
</property>
</bean>
<bean id="studentDao" class="com.springinaction.training.dao.hibernate.StudentDaoHibernate">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>

you can always use Spring’s autowire facility to implicitly wire your DAO beans. Now that you know how to wire a HibernateTemplate to your DAO objects,

The HibernateTemplate class provides some convenience methods that implicitly create a HibernateCallback instance for you.

public Student getStudent(Integer id) {
 return (Student) hibernateTemplate.load(Student.class, id);
}

public void updateStudent(Student student) {
 hibernateTemplate.update(student);
}

Subclassing HibernateDaoSupport:

Spring’s Hibernate ORM framework also comes with the convenience class HibernateDaoSupport that your DAO classes can subclass:
public class StudentDaoHibernate extends HibernateDaoSupport
implements StudentDao {

}

If you opt for this design, you need to wire in a SessionFactory—the HibernateDaoSupport class comes with this property. This class provides you with a convenience method, getHibernateTemplate(), to easily get an instance of a HibernateTemplate.It also has a getSession() and a closeSessionIfNecessary() method if, for some reason, you need to perform a Hibernate operation without using a HibernateTemplate.

你可能感兴趣的:(spring)