HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Configure JPA without persistence.xml

By Lokesh Gupta | Filed Under: JPA

If you are working on Spring project and using JPA for data access, then you probably have persistence.xml file as well in META-INF folder. If you want to get rid of persistence.xml and configure datasource, persistence units name and entity paths in spring’s application-context.xml only then you can do it as below while configuring entityManagerFactoryBean.

<bean id="entityManagerFactoryBean"	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	
		<property name="dataSource" ref="dataSource" />

		<property name="persistenceUnitName" value="demoJPAUnit" />
		
		<property name="packagesToScan">
			<list>
				<value>com.howtodoinjava.jpa.demo.entity</value>
			</list>
		</property>

		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
		</property>
		
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.archive.autodetection">class,hbm</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
			</props>
		</property>
	</bean>

These two properties “persistenceUnitName” and “packagesToScan” help you configuring persistence unit-name and entity lookup paths.

For you reference, complete aplication-context.xml is configured as below.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<context:component-scan base-package="com.howtodoinjava.jpa.demo" />

	<bean id="entityManagerFactoryBean"	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	
		<property name="dataSource" ref="dataSource" />

		<property name="persistenceUnitName" value="demoJPAUnit" />
		
		<property name="packagesToScan">
			<list>
				<value>com.howtodoinjava.jpa.demo.entity</value>
			</list>
		</property>

		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
		</property>
		
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.archive.autodetection">class,hbm</prop>
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
			</props>
		</property>
	</bean>

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="password" />
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
	</bean>

	<tx:annotation-driven />

</beans>

Drop me your comments and suggestions.

Happy Learning !!

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

3
Leave a Reply

This comment form is under antispam protection
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Harish

What if I want to scan entity from external jar file ?

com.howtodoinjava.jpa.demo.entity

What should be the change ?

Vote Up0Vote Down  Reply
8 months ago
Sri Krishna

This worked with JPA + Dynamic Web Integration …
Adding JPA Project to Dynamic Web Project and appContext.xml Worked..
Thanks

Vote Up0Vote Down  Reply
3 years ago
Binh Thanh Nguyen

Thanks, nice tip

Vote Up0Vote Down  Reply
4 years ago

Search Tutorials

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz