Re: Dropwizard-hibernate and EntityManager

1,531 views
Skip to first unread message

Frank Grimes

unread,
Apr 29, 2013, 10:09:42 AM4/29/13
to Jonathan Brownell, dropwiz...@googlegroups.com
I played around a bit more with this but still haven't found a way to create an EntityManagerFactory without having a minimal persistence.xml file to specify the PersistenceUnit name.

e.g. 

        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();
 }

This would be a problem if multiple Hibernate/JPA bundles within one Dropwizard application are required. (I think multiple PersistenceUnits would be desired)

Also, I didn't need to explicitly specify which annotated Entity classes I have. 
I think it may be doing classpath scanning. (I think that could slow down application startup)

Frank Grimes


On 2013-04-26, at 10:05 AM, Frank Grimes <frankg...@gmail.com> wrote:

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 the EntityManagerFactory 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 on Configuration due to move towards new SessionFactory building methodology. See HHH-6181 and HHH-6159 for details

According 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.html


Regards,

Frank Grimes


On 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


frankgrimes97

unread,
May 7, 2013, 10:03:53 AM5/7/13
to dropwiz...@googlegroups.com, Jonathan Brownell
I just got the following reply on the Hibernate lists... looks like there is a way to do it, will need to find time to play around and test it out:

Lee Torrence

unread,
Dec 15, 2013, 5:34:52 PM12/15/13
to dropwiz...@googlegroups.com, Jonathan Brownell
Frank - were you ever able to integrate this code with HibernateBundle?
Reply all
Reply to author
Forward
0 new messages