<persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"><persistence-unit name="MyPersistenceUnit"/></persistence>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.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();}
Hi Jonathan,Unfortunately I haven't had much time to look into this.I also am new to JPA and used Hibernate years ago in the 2.1.x/3.0.x days, only recently returning to use it along with Dropwizard.I did find the following example code to create an EntityManager programmatically using Hibernate's APIs: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.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(EntityA.class);cfg.addAnnotatedClass(EntityB.class);EntityManagerFactory factory = cfg.buildEntityManagerFactory();return factory.createEntityManager();}However, I see that Ejb3Configuration is now deprecated and to be removed in Hibernate 5:Deprecated.
Direct usage of this class has never been supported. Instead, the application should obtain reference to theEntityManagerFactory
as outlined in the JPA specification, section 7.3 Obtaining an Entity Manager Factory based on runtime environment. Additionally this class will be removed in Hibernate release 5.0 for the same reasoning outlined onConfiguration
due to move towards newSessionFactory
building methodology. See HHH-6181 and HHH-6159 for detailsAccording to the spec, the following should be used but in looking at it briefly I don't see a way to add annotated classes programmatically:http://docs.oracle.com/javaee/6/api/javax/persistence/Persistence.htmlRegards,Frank GrimesOn 2013-04-25, at 5:09 PM, Jonathan Brownell <caden...@gmail.com> wrote:Frank, I saw your mail to the dropwizard list recently, and I'm
interested in doing the same thing (producing an EntityManager from
the dropwizard-hibernate API). Have you been able to cook up any
examples of how this would work? I'm relatively new to JPA and was
interested to hear how you are going about dealing with this.
-JB