以编程方式创建EntityManagerFactory(不带persistence.xml文件),带有带注释的类

时间:2013-12-16 18:18:28

标签: hibernate entitymanager

现在我正在创建这样的EntityManagerFactory

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

但是正如我在javadocs中看到的那样,Ejb3Configuration已被弃用,我不应该使用它。我应该根据JPA spec第7.3节使用Persistence.createEntityManagerFactory()。但是我只能传递一些属性,但是我能以某种方式添加带注释的类吗?

1 个答案:

答案 0 :(得分:2)

请使用Spring注释找到MySQL的等效配置类:

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

要使用嵌入式数据库(如HSQL,H2或Derby)执行测试,您可以添加另一个数据源bean:

@Bean(name = "embeddedDatabase")
public DataSource embeddedDataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).build();
}