Created
August 22, 2012 19:45
-
-
Save timoteoponce/3428727 to your computer and use it in GitHub Desktop.
Create an EntityManager programatically in JPA1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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