32

I'm trying to set up spring xml configuration without having to create a futher persistence.xml. But I'm constantly getting the following exception, even though I included the database properties in the spring.xml

 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\me\workspace\app\src\main\webapp\WEB-INF\applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml} 

spring.xml:

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> </bean> 

What am I missing here?

4 Answers 4

41

From Spring Guide Accessing Data with JPA

@Configuration @EnableJpaRepositories public class Application { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder().setType(H2).build(); } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(jpaVendorAdapter); lef.setPackagesToScan("hello"); return lef; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(false); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setDatabase(Database.H2); return hibernateJpaVendorAdapter; } 

Spring Boot

With Spring Boot enabled application this is even easier:

Sample application.yaml

spring: datasource: url: jdbc:h2:mem:test username: sa password: sa driver-class-name: org.h2.Driver jpa: database: H2 show-sql: false hibernate: format_sql: true ddl-auto: auto 
Sign up to request clarification or add additional context in comments.

1 Comment

this solution works for me, except in the instructions above, setPackagesToScan("package name"), where "package name" is the package include your entity classes, see docs.spring.io/spring/docs/current/javadoc-api/org/… for reference
31

Specify the "packagesToScan" & "persistenceUnitName" properties in the entityManagerFactory bean definition.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="persistenceUnitName" value="myPersistenceUnit" /> <property name="packagesToScan" > <list> <value>org.mypackage.*.model</value> </list> </property> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> </props> </property> </bean> 

Note that this is for Spring version > 3.1

6 Comments

Do you know to configure Composite Persistent Unit and its members without persistence.xml file in the same way needed by you.
does it allow us to load the entityManager with spring like : @PersistenceContext private EntityManager em;
I get "No persistance provider specified in EntityManagerConfiguration..." exception. Did you miss it in your answer?
What is the use of line <property name="persistenceUnitName" value="myPersistenceUnit" /> , Where is 'myPersistenceUnit' is exists??
But this is a xml config!
|
4

MariuszS' answer is good except that the entityManagerFactory method should return EntityManagerFactory. To do that it can be written like this:

@Bean public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(jpaVendorAdapter); lef.setPackagesToScan("hello"); return lef.object(); } 

For future audience: below code worked:

@Bean (name = "entityManagerFactory") public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(jpaVendorAdapter); lef.setPackagesToScan("*.models*"); lef.afterPropertiesSet(); // It will initialize EntityManagerFactory object otherwise below will return null return lef.getObject(); } 

Comments

2

Assuming that you have a PersistenceProvider implementation (e.g. org.hibernate.jpa.HibernatePersistenceProvider), you can use the PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) method to bootstrap an EntityManagerFactory without needing a persistence.xml.

However, it's annoying that you have to implement the PersistenceUnitInfo interface, so you are better off using Spring or Hibernate which both support bootstrapping JPA without a persistence.xml file.

this.nativeEntityManagerFactory = provider.createContainerEntityManagerFactory( this.persistenceUnitInfo, getJpaPropertyMap() ); 

Where the PersistenceUnitInfo is implemented by the Spring-specific MutablePersistenceUnitInfo class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.