2

Hi does anyone know what the preferred way to do programmatically create a JPA EntityManagerFactory impl from Hibernate in Hibernate 4+?

I used hsql to do this, but its depricated;

Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
props.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver"); 
props.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:aname");
props.setProperty("hibernate.connection.username", "sa" );  
props.setProperty("hibernate.connection.password", "" );        

Ejb3Configuration config3 = new Ejb3Configuration();
config3.setProperties(props);
config3.addFile("src/org/adligo/i/storage/entities/keyValue.xml");
impl = config3.buildEntityManagerFactory();

This seems to be the new way, but I keep getting NPEs;

impl = new EntityManagerFactoryImpl(transactionType, false, 
                    clazz, config, serviceRegistry, "i_storage_tests_persistence_unit");

TIA, Scott

  • Can refer http://stackoverflow.com/q/9315593/366964 for creating EntityManagerFactory without xml – Nayan Wadekar Apr 02 '12 at 17:57
  • Ok that seems to work, however I still have a issue that I can't seem to solve with the standard api `Properties props = new Properties(); props.setProperty(AvailableSettings.CONNECTION_PROVIDER, "org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider"); Ejb3Configuration config3 = new Ejb3Configuration(); config3.setDataSource(ds);` I am using this kind of code to inject the data source for junit testing of a single threaded apache DBCP pool, however I will probably use c3p0 in production. – user1303800 Apr 17 '12 at 17:41
  • Also I am having a issue using the hibernate mappings with this (which I need because they work better),`InputStream in = KeyValue.class.getResourceAsStream("keyValue.xml"); config3.addInputStream(in);` – user1303800 Apr 17 '12 at 17:55
  • You can update post with the above details & issue encountered, so that it may help others to get better idea of it – Nayan Wadekar Apr 17 '12 at 18:10

0 Answers0