JPA没有persistence.xml [英] JPA without persistence.xml
问题描述
我试图开始使用Guice Persist和JPA,它建议通过persistence.xml使用配置。来自原生Hibernate背景,其中配置是以编程方式获得的,是否有一种简单的方法来配置没有persistence.xml文件的JpaPersistModule,或者一个rump persistence.xml总是必须存在?
如果不存在这样的选项,则可能是我可能必须使用PersistenceProvider(假设默认以某种方式解析persistence.xml)。关于使用JPA SPI的任何教程?
不需要 persistence.xml
$ $ p $ $ $ c $ @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);
返回dataSource;
public LocalContainerEntityManagerFactoryBean entityManagerFactory()抛出PropertyVetoException {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setJpaVendorAdapter(vendorAdapter());
em.setPersistenceUnitName(cimtoPU);
em.setJpaPropertyMap(getJpaProperties());
返回em;
}
public Map< String,?> getJpaProperties(){
返回新的HashMap< String,Object>();
$ b @Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
返回transactionManager;
public JpaVendorAdapter vendorAdapter(){
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.MYSQL);
vendorAdapter.setDatabasePlatform(org.hibernate.dialect.MySQL5Dialect);
vendorAdapter.setShowSql(true);
返回vendorAdapter;
注意: com.demoJPA .model
包必须包含您的实体类。
I'm trying to get started with using Guice Persist and JPA, which recommends using configuration via persistence.xml. Coming from a native Hibernate background where configuration was obtained programmatically, is there a simple way to configure a JpaPersistModule without a persistence.xml file, or will a rump persistence.xml always have to exist?
If no such option exists, it might be the case where I might have to play around with PersistenceProvider (assuming the "default" parses persistence.xml somehow). Any tutorials on working with the JPA SPI?
There is no need for persistence.xml
if you are using a Spring version higher than 3.1 and you have already defined your entities classes.
@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;
}
}
Note: com.demoJPA.model
package must contain your entities classes.
这篇关于JPA没有persistence.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!