JPA没有persistence.xml

编程入门 行业动态 更新时间:2024-10-21 23:07:27
本文介绍了JPA没有persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图开始使用Guice Persist和JPA,它建议通过persistence.xml使用配置。来自原生Hibernate背景,其中配置是以编程方式获得的,是否有一种简单的方法来配置没有persistence.xml文件的JpaPersistModule,或者一个rump persistence.xml总是必须存在?

如果不存在这样的选项,则可能是我可能必须使用PersistenceProvider(假设默认以某种方式解析persistence.xml)。关于使用JPA SPI的任何教程?

解决方案

不需要 persistence.xml

code>如果你使用的Spring版本高于3.1,并且你已经定义了实体类。 $ $ 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

本文发布于:2023-10-25 14:04:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JPA   persistence   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!