开发者

Using Hibernate with Spring 3

My current web development tool is Spring 3, I've used Hibernat开发者_如何学编程e before, I'm actually quite familiar with it after I have access to the annotations and entities, and the session object. However, this will be my first time needing to actually set it up from scratch.

Currently I have a datasource that I have used for JDBCTemplate, and I want to reuse that for Hibernate.

Unfortunately, the more I read about Hibernate, the more confused I get. There are many different ways to configure it, and things have changed quite a bit since some of the tutorials. Most confusing is that sometimes a persistence.xml file is used, sometimes a hibernate config XML file.

Then there are session factories, but then there are also entity manager factories. Could someone please distill some of this information so I have a bit more of a clear path ahead of me.

My only real requirements are that I can use the annotations with Hibernate. I'd like to have support for transactions too.

I know this question is somewhat vague, but it's because I've been unable to actually find a solid "tutorialized" solution that is up to date with spring 3 that I trust.


Currently I have a datasource that I have used for JDBCTemplate, and I want to reuse that for Hibernate.

No problem. I'd just mention that accessing the same tables through both Hibernate and JDBC will require some precautions.

(...) Most confusing is that sometimes a persistence.xml file is used, sometimes a hibernate config XML file.

The hibernate.cfg.xml is Hibernate native configuration file. The persistence.xml is the JPA (the standardized Java Persistence API) configuration file. If you decide to go the JPA way (and this would be my recommendation), you usually use the persistence.xml only. And if you use Spring, most configuration will actually end up in Spring configuration file in any case.

Then there are session factories, but then there are also entity manager factories. Could someone please distill some of this information so I have a bit more of a clear path ahead of me.

Same story as above. The SessionFactory (and Session) are classes from Hibernate's native API while the EntityManagerFactory and EntityManager are somehow the JPA "counterparts". If you go the JPA way, you'll deal with the EntityManagerFactory and EntityManager.

My only real requirements are that I can use the annotations with Hibernate. I'd like to have support for transactions too.

As already mentioned, my suggestion would be to use the standardized JPA API (and to use Hibernate proprietary extensions only if strictly required). For transactions, use Spring.

The section 12.6. JPA of the documentation covers everything you'll need to configure Spring (I suggest to use the LocalContainerEntityManagerFactoryBean that you'll feed with your datasource).

For the data access layer (the DAOs) either use the JpaTemplate and JpaDaoSupport classes or implement DAOs based on plain JPA using an injected EntityManagerFactory or EntityManager (I personally use the later approach).

All these references to the Spring 2.5 documentation are still valid with Spring 3.0. Just in case, here is a not too old tutorial that could help you to get started.

See also

  • Similarity and difference between JPA and Hibernate
  • Difference Hibernate 3.5 / JPA 2.0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜