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

Leave a Reply

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial