Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create EntityManagerFactory programatically (without persistence.xml file) with annotated classes

Now I am creating EntityManagerFactory like this:

    Map<String, String> properties = ImmutableMap.<String, String>builder()
        .put(DRIVER, "com.mysql.jdbc.Driver")
        .put(DIALECT, "org.hibernate.dialect.MySQL5Dialect");
        .put(USER, dbUsername)
        .put(PASS, dbPassword)
        .put(URL, dbConnectionUrl)
        //Some more properties
        .build();

    Ejb3Configuration cfg = new Ejb3Configuration();

    cfg.configure(properties);

    cfg.addAnnotatedClass(AuditEntry.class);
    cfg.addAnnotatedClass(LastWrittenEventId.class);
    //Some more annotated classes

    return cfg.createEntityManagerFactory();

However as I can see in javadocs, Ejb3Configuration is deprecated and I shouldn't use it. I should use Persistence.createEntityManagerFactory() according to JPA spec section 7.3. But then I can pass only some properties, but can I add annotated classes somehow?

like image 555
amorfis Avatar asked Dec 16 '13 18:12

amorfis


1 Answers

Please find the equivalent configuration class for MySQL with Spring annotations :

package config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {

    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        return properties;
    }

    @Bean
    public DataSource dataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("foo.bar");
        factory.setDataSource(dataSource());
        factory.setJpaProperties(jpaProperties());

        return factory;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(emf);

        return txManager;
    }
}

Spring dependencies :

 <dependencies>
    ...  
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    ...
</dependencies>

To perform tests with an embedded database such as HSQL, H2 or Derby, you can add another datasource bean :

@Bean(name = "embeddedDatabase")
public DataSource embeddedDataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).build();
}
like image 175
André Blaszczyk Avatar answered Nov 17 '22 09:11

André Blaszczyk