I am investigating using JPA for my Data Access code. I am trying to write the business layer and data access layer so it will work in a web application and a Java SE application. Therefore I cannot use container managed persistence context. Most of my seraches on using JPA show examples in a container managed enviroment.
At the moment I get the EntityManagerFactory everytime I create a new instace of the service class. For every operation (ie add, get, etc) I open an EntityManager, start a transaction perform operations, commit and then close the EntityManager and the EntityManagerFactory. I would like the benifits of having a manged persistence context outside of the Java EE environment.
Are there any best practices when not using a container managed context? Are there any Java EE independent persistence context managers? Are there any recommended patterns?
Thanks,
Al
Update
Thank you eveyone for the info. Everything was very useful.
I’m not sure about the best practices surrounding this, but I have spent a lot of time trying to make something like this work.
Basically you will need something to construct an
EntityManagerwith. I’ve always used Spring for this. Their documentation has a big section on this. You can choose to use a LocalEntityManagerFactoryBean, in which case the markup would look like (from aforementioned documentation):This is, in principle, not encouraged, because it comes back to haunt you when you try to change data sources later on. However, I find it’s very unlikely you’ll run into the limitations of this for most webapps up to a certain size.
Configuration of your datasource can then be done through hibernate specific properties in your persistence unit (persistence.xml in META-INF/ directory):
To use this, if you’re not using spring already, you can just grab an instance of the
EntityManagerFactoryfrom an application context and go from there (i.e.context.getBean("myEmf")).More control is possible with
LocalContainerEntityManagerFactoryBean, this one allows you to configure a data source. In principle the example from the docs should work but I found when I did this I had to specify the Hibernate persistence provider. You need a persistence.xml but it really only needs a default persistence unit and very basic configuration (perhaps identifying the dialect, e.g. if you’re using hibernate with oracle 10g):There’s examples elsewhere in the spring docs on how to configure a
BasicDataSourcefrom Apache dbcp, which will give you pooling of connections as well.As for best practices, JPA really isn’t easy to work with outside of a full application server environment. There’s all sorts of issues when trying to tune performance too where you’ll find yourself salivating over Hibernate features that aren’t available to you in JPA, and chances are your queries will end up not strictly compliant with the JPA specs anyway. If you’re not using container managed, it would probably be a lot easier and saner to just use the Hibernate APIs directly.