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 EntityManager
with. 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):
<bean id="myEmf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPersistenceUnit"/>
</bean>
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):
<property name="hibernate.connection.driver_class" value="com.company.driver" />
<property name="hibernate.connection.url" value="some:jdbc@string" />
<property name="hibernate.connection.username" value="myuser" />
<property name="hibernate.connection.password" value="mypassword" />
To use this, if you're not using spring already, you can just grab an instance of the EntityManagerFactory
from 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):
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDatasource" />
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence" />
</property>
</bean>
There's examples elsewhere in the spring docs on how to configure a BasicDataSource
from 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.