java – 没有persistence.xml的JPA
发布时间:2020-12-15 04:28:19 所属栏目:Java 来源:网络整理
导读:我正在尝试使用Guice Persist和JPA,它建议通过persistence.xml使用配置.来自本地Hibernate背景,其中以编程方式获取配置,是否有一种简单的方法来配置没有persistence.xml文件的JpaPersistModule,或者rump persistence.xml是否总是存在? 如果不存在这样的选项
|
我正在尝试使用Guice Persist和JPA,它建议通过persistence.xml使用配置.来自本地Hibernate背景,其中以编程方式获取配置,是否有一种简单的方法来配置没有persistence.xml文件的JpaPersistModule,或者rump persistence.xml是否总是存在?
如果不存在这样的选项,可能就是我可能必须使用PersistenceProvider(假设“默认”以某种方式解析persistence.xml).有关使用JPA SPI的任何教程吗? 解决方法
如果您使用的是高于3.1的
Spring版本并且已经定义了实体类,则不需要persistence.xml.
@Configuration
@ComponentScan(basePackages = { "com.demoJPA.model" })
@EnableTransactionManagement
public class DemoJPAConfig {
@Bean
public DataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("org.gjt.mm.mysql.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/cimto");
dataSource.setUser("user");
dataSource.setPassword("pass");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setJpaVendorAdapter(vendorAdapter());
em.setPersistenceUnitName("cimtoPU");
em.setJpaPropertyMap(getJpaProperties());
return em;
}
public Map<String,?> getJpaProperties() {
return new HashMap<String,Object>();
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
public JpaVendorAdapter vendorAdapter() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.MYSQL);
vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
vendorAdapter.setShowSql(true);
return vendorAdapter;
}
}
注意:com.demoJPA.model包必须包含您的实体类. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
