Hi All,
We found the following example of how to create an EntityManagerFactory which seems to work well:
Code:
Properties properties = new Properties();
properties.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
properties.put("hibernate.connection.username", "sa");
properties.put("hibernate.connection.password", "");
properties.put("hibernate.connection.driver_class", "org.h2.Driver");
properties.put("hibernate.connection.url", "jdbc:h2:.");
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
Ejb3Configuration cfg = new Ejb3Configuration();
cfg.addProperties(properties);
cfg.addAnnotatedClass(ReportBuild.class);
EntityManagerFactory factory = cfg.buildEntityManagerFactory();
The problem is that Ejb3Configuration now appears to be deprecated and slated for removal.
Is there a way to accomplish the same thing using non-deprecated APIs?
Specifically, we want no hibernate.hbm.xml or persistence.xml defined and also want to avoid classpath scanning, hence the explicit calls to cfg.addAnnotatedClass().
I tried the following, but it does classpath scanning and also requires a nearly empty persistence.xml just so that the persistent-unit name can be found (otherwise it errors out):
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="MyPersistenceUnit"/>
</persistence>
Code:
Properties properties = new Properties();
properties.put("javax.persistence.provider", "org.hibernate.ejb.HibernatePersistence");
properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
properties.put("hibernate.connection.username", "sa");
properties.put("hibernate.connection.password", "");
properties.put("hibernate.connection.driver_class", "org.h2.Driver");
properties.put("hibernate.connection.url", "jdbc:h2:.");
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
EntityManagerFactory factory = Persistence.createEntityManagerFactory("MyPersistenceUnit", properties);
return factory.createEntityManager();
Thanks,
Frank Grimes