views:

194

answers:

1

My current web development tool is Spring 3, I've used Hibernate 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.

+2  A: 

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

Pascal Thivent
I prefer using the session object for hibernate query creation, if I go the way of JPA, am I no longer able to do that? What do I get by sticking to JPA over hibernate?
walnutmon
edited: nevermind my original question, you've answered that in another post. So I guess the only question then, is if you'd recommend JPA with hibernate, how do I go about setting that up with spring 3? The tutorials, and hibernate manual set up hibernate with a session factory, not a entity manager factory
walnutmon
@jboyd: In short, JPA is a subset so you actually loose some parts (the gap is small since JPA 2.0 though). But you get vendor independence (you could use Hibernate, EclipseLink, etc as JPA provider) and since JPA is a standardized API, you could reuse the JPA knowledge in another context. I would go for JPA 2.0 for any new development (by the way, the EntityManager is very close to the Session). At the end, the decision is up to you of course.
Pascal Thivent
@jboyd: I covered all this in my answer and provided links to the Spring documentation (as I wrote, these links are still valid, the ORM part didn't really change in Spring 3.0).
Pascal Thivent
I've read the spring documentation, and every JPA portion I've read requires a "persistence unit". I recall having these when I used to use hibernate, but I don't see much discussion on how to set them up, and honestly, I don't understand why I need anything more than a database connection (datasource).
walnutmon
@jboyd: If you don't feel comfortable with using JPA, if you don't have the time to learn the basic concepts of JPA, then use Hibernate and read the the section [12.2. Hibernate](http://static.springsource.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate) to configure a `SessionFactory` in Spring. I can't give you a "ready to use" Spring configuration, there are just too many options with Spring.
Pascal Thivent
@jboyd: Just in case, I've added a relatively up-to-date and quick tutorial to my answer. Good luck.
Pascal Thivent
Thanks, and don't get me wrong, at some point I will look more deeply into these topics, but right now I'm looking to use such a small set of functionality that spending more than a few hours to configure my ORM would make using the ORM counterproductive. It's unfortunate that the creators of these products don't provide ready to use configurations, but that is part of using Java, what we get with cheap flexibility, comes at the price of a lot of knowledge needed to perform very simple tasks at times
walnutmon
@jboyd: I understand, don't worry. And you're welcome.
Pascal Thivent