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:
Thanks!
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With