简体   繁体   English

没有persistence.xml的JPA

[英]JPA without persistence.xml

I'm trying to get started with using Guice Persist and JPA, which recommends using configuration via persistence.xml.我正在尝试开始使用 Guice Persist 和 JPA,它建议通过 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?来自以编程方式获取配置的本机 Hibernate 背景,是否有一种简单的方法来配置 JpaPersistModule 而不需要 persistence.xml 文件,或者是否必须始终存在 rump persistence.xml?

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).如果不存在这样的选项,那么我可能不得不使用 PersistenceProvider(假设“默认”以某种方式解析persistence.xml)。 Any tutorials on working with the JPA SPI?任何有关使用 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.如果您使用的是高于 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;
    }
}

Note: com.demoJPA.model package must contain your entities classes.注意: com.demoJPA.model包必须包含您的实体类。

Assuming that you have a PersistenceProvider implementation (eg Hibernate), you can use the PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) method to bootstrap an EntityManagerFactory without needing a persistence.xml .假设您有一个PersistenceProvider实现(例如 Hibernate),您可以使用PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map)方法来引导EntityManagerFactory而不需要persistence.xml

However, it's annoying that you have to implement the PersistenceUnitInfo interface, so you are better off using Spring or Hibernate which both support bootstrapping JPA without a persistence.xml file:但是,您必须实现PersistenceUnitInfo接口很烦人,因此最好使用 Spring 或 Hibernate,它们都支持在没有persistence.xml文件的情况下引导 JPA:

this.nativeEntityManagerFactory = provider.createContainerEntityManagerFactory(
    this.persistenceUnitInfo, 
    getJpaPropertyMap()
);

Where the PersistenceUnitInfo is implemented by the Spring-specific MutablePersistenceUnitInfo class.其中PersistenceUnitInfo由 Spring-specific MutablePersistenceUnitInfo类实现。

Depending on what you want to achieve and in what context (ApplicationServer vs CLI, CMT transactions vs EntityTransaction s), it may be possible to use JPA without a persistence.xml .根据您想要实现的目标以及在什么上下文中(ApplicationServer 与 CLI、 CMT 事务EntityTransaction s),可能可以在没有persistence.xml情况下使用 JPA。 I did this in a CLI Java application, where I had different databases with the same structure.我在 CLI Java 应用程序中执行此操作,其中我有具有相同结构的不同数据库。 For that I constructed the EntityManagerFactory manually.为此,我手动构建了EntityManagerFactory

PS: The config file is there to make your life easier, so if you can, just use it. PS:配置文件是为了让您的生活更轻松,所以如果可以,请使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM