Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Spring without persistence.xml?

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?

like image 869
membersound Avatar asked Jan 27 '14 13:01

membersound


People also ask

Does Spring boot need persistence xml?

Spring Boot will not search for or use a META-INF/persistence. xml by default. If you prefer to use a traditional persistence. xml , you need to define your own @Bean of type LocalEntityManagerFactoryBean (with an ID of 'entityManagerFactory') and set the persistence unit name there.

Is persistence xml required for JPA?

While Spring applications can bootstrap without needing an XML JPA configuration file, it's still important to understand the meaning of each configuration option since Spring also offers an alternative way when building a Java Persistence LocalContainerEntityManagerFactoryBean or the Hibernate-specific ...

Can we use Spring data JPA without Hibernate?

No, you cannot perform CRUD operations with JPA alone. As JPA is just a specification, you need the implementation to perform database operations. The implementations are provided by Hibernate, EclipseLink, Ibatis, etc.

What do you have to configure to use JPA with Spring How does Spring boot make this easier?

If we want to use JPA with MySQL database, we need the mysql-connector-java dependency. We'll also need to define the DataSource configuration. We can do this in a @Configuration class or by using standard Spring Boot properties. Spring Boot will automatically configure a data source based on these properties.


1 Answers

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 
like image 158
MariuszS Avatar answered Sep 20 '22 21:09

MariuszS