JPA without persistence.xml

I am trying to start by using Guice Persist and JPA, which recommends using configuration via persistence.xml. Based on the native Hibernate background where the config was obtained programmatically, is there an easy way to set up the JpaPersistModule without the persistence.xml file, or will there be a persistence.xml file?

If no such option exists, maybe it might be a case where I might need to play with the PersistenceProvider (assuming the "default" parises persistence.xml somehow). Any tutorials on working with JPA SPI?

+3


source to share


3 answers


No need persistence.xml

if you are using Spring version above 3.1 and you have already defined your entities.

@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

the package should contain object classes.

+4


source


Assuming you have an implementation PersistenceProvider

(like Hibernate), you can use PersistenceProvider # createContainerEntityManagerFactory (PersistenceUnitInfo info, map map) for bootstrapping EntityManagerFactory

unnecessarily persistence.xml

.

However, it is annoying that you have to implement an interface PersistenceUnitInfo

, so you are better off using Spring or Hibernate which support JPA loading without a file persistence.xml

:

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

      



Where PersistenceUnitInfo is implemented by Spring-specific MutablePersistenceUnitInfo .

Check out this article for a good demonstration of how you can achieve this goal with Hibernate.

+3


source


Depending on what you want to achieve and in what context (ApplicationServer vs CLI, CMT transactions vs EntityTransaction s), it is possible to use JPA without persistence.xml

. I did this in a Java CLI application where I had different databases with the same structure. For this I have built by EntityManagerFactory

hand.

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

0


source







All Articles