Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating Hibernate to JPA without annotations

I have a large non-Java EE, JSF-based web app project. Our system is layered (in the source code sense): there's a data model package, building on that is the DAO package. We are using Hibernate's XML configuration mapping exclusively in the DAO package. We really don't want to muddle the data model with annotations, but aren't wedded to Hibernate specifically (except that the mapping is quite complex).

I'm strongly considering making a move towards Java EE and building our DAO objects as EJBs. But as we're unwilling to discard Hibernate's XML, this leads me to several questions:

  • Is it possible to use Hibernate with JPA without JPA annotations on the model?
  • If not, is it possible for my EJBs to behave transactionally with Hibernate anyway? I think this is called JTA support, but not sure. I like the idea of getting transactions "for free"; right now we have a custom-coded phase listener I'd like to remove which handles Hibernate transactions.
  • Is there a way to migrate from Hibernate's XML mapping configuration to some sort of JPA XML mapping? I don't see a way to do this, but obviously it would be better to reduce our coupling to Hibernate.

Thanks!

like image 371
Daniel Lyons Avatar asked Aug 02 '11 16:08

Daniel Lyons


2 Answers

Is it possible to use Hibernate with JPA without JPA annotations on the model?

Yes, this is quite possible. You can specify all pertinent attributes of your object model classes, in JPA standard mapping file - orm.xml. Your can also have your own mapping files, but you'll need to specify them in the persistence.xml.

If not, is it possible for my EJBs to behave transactionally with Hibernate anyway? I think this is called JTA support, but not sure.

If you are using EJBs, you'll find container managed transactions to be quite useful. All you need to do is to annotate your EJBs with the required @TransactionManagement and @TransactionAttribute annotations, and the container will automatically manage the transactions for you, including committing the transaction at the end of the method (if applicable), and rolling back the transaction in the event of a runtime exception or an application exception.

All EntityManager instances, i.e. all persistence contexts, injected into such EJBs, would automatically be associated with the underlying JTA transaction. Note, that this is true only if you allow the container to inject EntityManager instances. If you manage the persistence contexts yourself (by creating them from EntityManagerFactory instances), then you might have to invoke EntityManager.joinTransaction() to associated all the work performed in the persistence context with the underlying JTA transaction. This is necessary as the EntityManager typically associates itself with the underlying JTA transaction, if you have configured the persistence context to use JTA datasources. If no JTA transaction exists, then no such association will occur, and therefore you will have to join with an active JTA transation if you want to flush the changes to a database.

Is there a way to migrate from Hibernate's XML mapping configuration to some sort of JPA XML mapping?

I'm unaware of any such tool as I've never had to migrate a project, but that does not mean that there are none to serve this purpose. You might want to take a look at the Hibernate Tools project, which does support JPA, although I'm unsure of whether it will allow you to convert from the Hibernate XML format to the JPA format.

like image 177
Vineet Reynolds Avatar answered Nov 10 '22 00:11

Vineet Reynolds


Yes, JPA also supports XML mappings. Apart from the standard persistence.xml, you have orm.xml and other xml files declaring entities according to a JPA xml schema. Here is a reference.

As for transactional EJBs - yes, they can be transactional (with or without JPA annotations on the model)

like image 20
Bozho Avatar answered Nov 10 '22 00:11

Bozho