How to configure Spring without persistence.xml?

54,037

Solution 1

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

Solution 2

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

Solution 3

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();
}

Solution 4

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.

Share:
54,037
membersound
Author by

membersound

JEE + Frameworks like Spring, Hibernate, JSF, GWT, Vaadin, SOAP, REST.

Updated on January 11, 2021

Comments

  • membersound
    membersound over 3 years

    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?

  • Ketan
    Ketan about 10 years
    Do you know to configure Composite Persistent Unit and its members without persistence.xml file in the same way needed by you.
  • herau
    herau almost 10 years
    does it allow us to load the entityManager with spring like : @PersistenceContext private EntityManager em;
  • Vadim Kirilchuk
    Vadim Kirilchuk almost 9 years
    I get "No persistance provider specified in EntityManagerConfiguration..." exception. Did you miss it in your answer?
  • Ashish Ratan
    Ashish Ratan over 7 years
    What is the use of line <property name="persistenceUnitName" value="myPersistenceUnit" /> , Where is 'myPersistenceUnit' is exists??
  • Ario
    Ario almost 7 years
    But this is a xml config!
  • IcyBrk
    IcyBrk about 6 years
    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
  • Shubhi224
    Shubhi224 over 5 years
    @Zaki but for persistenceUnitName, don't we need to define it in persistence.xml?