HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / JPA / Configure JPA without persistence.xml

Configure JPA without persistence.xml

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 !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

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

Feedback, Discussion and Comments

  1. Harish

    April 1, 2019

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

    com.howtodoinjava.jpa.demo.entity

    What should be the change ?

  2. Sri Krishna

    November 3, 2016

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

  3. Binh Thanh Nguyen

    May 5, 2015

    Thanks, nice tip

Comments are closed on this article!

Search Tutorials

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

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 © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)