|
From: <jue...@we...> - 2003-06-11 18:38:53
|
Hi Dmitriy,
No problem, I'll outline the basics. I assume some Hibernate and some =
Spring transaction knowledge.
1.
Register a Hibernate SessionFactory, either via LocalSessionFactoryBean =
(with an optional config "location" parameter, the default is a =
"hibernate.cfg.xml" in the classpath) or via JndiObjectFactoryBean (only =
necessary for the Hibernate JCA connector):
<bean id=3D"sessionFactory" =
class=3D"com.interface21.orm.hibernate.LocalSessionFactoryBean"/>
2.
Pass the SessionFactory reference to your data access object (assuming a =
sessionFactory property):
<bean id=3D"hibernateTest" class=3D"werk3.example.HibernateTest">
<property name=3D"sessionFactory"><ref =
bean=3D"sessionFactory"/></property>
</bean>
3.
In your data access object, use HibernateTemplate like as follows (the =
executeFind method is for a returned List, the general execute for a =
returned Object):
HibernateTemplate hibernateTemplate =3D new =
HibernateTemplate(this.sessionFactory);
List result =3D hibernateTemplate.executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException =
{
return session.find("from to in class werk3.example.TestObject");
}
});
A HibernateTemplate instance is threadsafe and reusable, thus it will =
typically be kept as instance of the data access object. In case of =
exceptions, HibernateTemplate will throw according (unchecked) =
DataAccessException subclasses. Therefore, a client of such a data =
access object does not need to know that the object uses Hibernate at =
all.
4.
If you want to use transactions, use Spring's transaction infrastructure =
with either JtaTransactionManager or HibernateTransactionManager. Taking =
part in Spring-managed JTA transactions just requires configuring =
Hibernate to access a transactional DataSource (in "hibernate.cfg.xml") =
- no special Hibernate setup necessary (unlike standard Hibernate), even =
for proper JVM-level caching!
HibernateTransactionManager would be registered as follows:
<bean id=3D"transactionManager" =
class=3D"com.interface21.orm.hibernate.HibernateTransactionManager">
<property name=3D"sessionFactory"><ref =
bean=3D"sessionFactory"/></property>
<!-- <property name=3D"dataSource"><ref =
bean=3D"dataSource"/></property> -->
</bean>
Transaction begins by TransactionTemplate or TransactionInterceptor then =
create a new Session with an active Hibernate transaction for the given =
SessionFactory, and bind it to the current thread. HibernateTemplate and =
any code using SessionFactoryUtils.getSession will automatically take =
part in such transactions.
The optional "dataSource" property takes a JDBC DataSource that =
HibernateTransactionManager will export a Hibernate Session's JDBC =
connection for, using the same mechanism as =
DataSourceTransactionManager. Obviously, the DataSource must be the =
same one that Hibernate uses. JdbcTemplate and any code using =
DataSourceUtils will automatically take part in such transactions, so =
this effectively allows for transactions spanning one DataSource but =
both Hibernate access and plain JDBC access.
5.
Changing or mixing data access tools is quite easy. Your data access =
object's client interface does not have to change when you switch =
between Hibernate, JDO, and plain JDBC - or mix various of them in =
distinct implementations. You just have to care for a proper transaction =
strategy, be it JTA or HibernateTransactionManager's DataSource support.
BTW, Spring's JDO support is very similarly modelled to the Hibernate =
one. There's LocalPersistenceManagerFactoryBean, JdoTemplate, =
JdoTransactionManager. Of course, JdoTemplate throws DataAccessException =
subclasses too.
A final note: I've used "Data access object" as a quite generic term. Of =
course, you don't have to separate DAOs from your business objects if =
you don't want to. In small to mid-size projects, blending business and =
data access objects may be appropriate. In the end, the most important =
thing is a clear business API, IMHO.
Hope that helps,
Juergen
-----Original Message-----
From: Kopylenko, Dmitry [mailto:dko...@ac...]
Sent: Wednesday, June 11, 2003 2:04 PM
To: j=FCrgen h=F6ller [werk3AT]
Subject: Hibernate
Hello Juergen.
Would it be much trouble for you to provide a simple usage example of
Hibernate within the Spring?
Thanks,
Dmitriy.
|