gpt4 book ai didi

hibernate - 使用带注释的类以编程方式创建 EntityManagerFactory(没有 persistence.xml 文件)

转载 作者:行者123 更新时间:2023-12-04 08:49:07 24 4
gpt4 key购买 nike

现在我正在创建 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已弃用,我不应该使用它。我应该使用 Persistence.createEntityManagerFactory()根据 JPA spec第 7.3 节。但是我只能传递一些属性,但是我可以以某种方式添加带注释的类吗?

最佳答案

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

关于hibernate - 使用带注释的类以编程方式创建 EntityManagerFactory(没有 persistence.xml 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com