JPA without persistence.xml
Asked Answered
S

3

5

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?

Sikes answered 18/6, 2015 at 3:17 Comment(0)
S
4

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.

Stenophagous answered 18/6, 2015 at 14:14 Comment(1)
Additional note : there may be no compilation error, but make sure Spring support the matching hibernate version. E.g. Spring 4.2 is the first to support Hibernate 5.0, Spring 4.3 is the first to support Hibernate 5.2.Bokbokhara
P
3

Assuming that you have a PersistenceProvider implementation (e.g. Hibernate), you can use the PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) method to bootstrap an EntityManagerFactory without needing a 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:

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

Where the PersistenceUnitInfo is implemented by the Spring-specific MutablePersistenceUnitInfo class.

Pricilla answered 18/6, 2015 at 7:32 Comment(0)
A
0

Depending on what you want to achieve and in what context (ApplicationServer vs CLI, CMT transactions vs EntityTransactions), it may be possible to use JPA without a persistence.xml. I did this in a CLI Java application, where I had different databases with the same structure. For that I constructed the EntityManagerFactory manually.

PS: The config file is there to make your life easier, so if you can, just use it.

Alerion answered 18/6, 2015 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.