Skip to content

Instantly share code, notes, and snippets.

@timoteoponce
Created August 22, 2012 19:45
Show Gist options
  • Save timoteoponce/3428727 to your computer and use it in GitHub Desktop.
Save timoteoponce/3428727 to your computer and use it in GitHub Desktop.
Create an EntityManager programatically in JPA1
/**
* @return an in-memory HSQL entityManager
* @author Timoteo Ponce
*/
public static EntityManager createEntityManager() {
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.hsqldb.jdbcDriver");
properties.put("hibernate.connection.url", "jdbc:hsqldb:." );
properties.put("hibernate.dialect" ,"org.hibernate.dialect.HSQLDialect");
properties.put("hibernate.hbm2ddl.auto","create-drop");
properties.put("hibernate.show_sql","true");
properties.put("hibernate.format_sql" ,"true");
//
Ejb3Configuration cfg = new Ejb3Configuration();
cfg.addProperties(properties);
cfg.addAnnotatedClass(City.class);
cfg.addAnnotatedClass(LocalCustomer.class);
//
EntityManagerFactory factory = cfg.buildEntityManagerFactory();
return factory.createEntityManager();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment