3.1.12 JPA EntityManagerFactory bootstrapping without persistence.xml
In standard JPA, persistence units get defined through META-INF/persistence.xml files in specific jar files which will in turn get searched for @Entity classes. In many cases, persistence.xml does not contain more than a unit name and relies on defaults and/or external setup for all other concerns (such as the DataSource to use, etc). For that reason, Spring 3.1 provides an alternative: LocalContainerEntityManagerFactoryBean accepts a 'packagesToScan' property, specifying base packages to scan for @Entity classes. This is analogous to AnnotationSessionFactoryBean's property of the same name for native Hibernate setup, and also to Spring's component-scan feature for regular Spring beans. Effectively, this allows for XML-free JPA setup at the mere expense of specifying a base package for entity scanning: a particularly fine match for Spring applications which rely on component scanning for Spring beans as well, possibly even bootstrapped using a code-based Servlet 3.0 initializer.
Source: New Features and Enhancements in Spring 3.1
Here's my existing configuration:
This requires an extra META-INF/persistence.xml to make it work:
How do we improve this?
Based on the docs, we can remove the persistence.xml altogether. But how do we declare the extra configurations related to our ORM? And how does the entityManagerFactory know where our entities are?
Here's how:
1. Delete the META-INF/persistence.xml
2. Declare a packagesToScan property
3. Declare a jpaPropertyMap
Here's the final configuration:
If you want to try this with an actual project, you either create a new one, or you can play with my demo project Spring MVC: Integrating MySQL, MongoDB, RabbitMQ, and AJAX which has a Github repo.
That's all folks!
Share the joy:
|
Subscribe by reader Subscribe by email Share
Thank you for nice idea!
ReplyDeleteAwesome. Thank you for that.
ReplyDeleteNice article and i got few questions with this implementation without persistence.xml:
ReplyDeletea) Don't we miss the RESOURCE_LOCAL functionality by the removal of persistence.xml
b) Assume i have 2 datasources, so i would go with 2 bean definition of LocalContainerEntityManagerFactoryBean class. To obtain those instance of EntityManagerFactory in java class, we have to use @Autowired or @PersistenceContext. If @PersistanceContext how can we differentiate which instance has to be assigned.
Just type the name of the persistence unit inside the annotation @PersistenceContext(unitName = "name")
DeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletecore java training in Electronic City
Hibernate Training in electronic city
spring training in electronic city
java j2ee training in electronic city
This is my blog. Click here.
ReplyDeleteราคาต่อรอง ระวังราคาไหลหลอกทำให้ไขว้เขว…"
Quickbooks is the advanced accounting software to manage business data conveniently. The Quickbooks data transfer utility allows users to easily and quickly transfer data from one quickbook file to another file.
ReplyDeleteAmazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live.
ReplyDeletefast food restaurants
bbq chicken
chicken tikka
pepperoni pizza,
baguettes,
tandoori nan
fast food bradford
Really enjoyed this article.Much thanks again. Want more
ReplyDeletejava training
java online training
청송콜걸
ReplyDelete군포콜걸
군포콜걸
강릉콜걸
영양콜걸
춘천콜걸
하남콜걸
하남콜걸