0

I can't figure out what HibernateUtil is ... Is it required with JPA?

I use JPA with GWT , is this implementation sufficient?

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class EMF {
    private static final EntityManagerFactory emfInstance =
        Persistence.createEntityManagerFactory("default");

    private EMF() {}

    public static EntityManagerFactory get() {
        return emfInstance;
    }
}

And at the use:

public class AccountDao {

  public static final EntityManager entityManager() {
    return Emf.get().createEntityManager();
  }



    public void createAccount(Account account) {

        EntityManager em = entityManager();
        EntityTransaction tx = em.getTransaction();

        try {
          tx.begin(); 
          em.persist(account);
          tx.commit();
        } 
        catch (Throwable t) {
          t.printStackTrace();
          tx.rollback();
        } 
        finally {
          em.close();
        }
      }
    }

See this post (Gilead JPA configuration) please. I can't understand yet, how to use HibernateUtil, or HibernateJpaUtil, or PersistentBeanManager stuff ...

Community
  • 1
  • 1
kostas trichas
  • 2,923
  • 7
  • 28
  • 37

2 Answers2

2

Your implementation is pretty sufficient. I would put the factory in the servlet context, rather than making it static though.

But note an important thing here. The above code will work if you are using it purely on the server-side.

Since you are using GWT, it is possible (although I don't think it is rational) to use hibernate "stuff" on the client-side. For that you'd need gilead, where you will need the forementioned utilities.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I don't know how to configure Gilead with JPA. If i use Gilead, EMF.java remains the same, or has to be configured=with HibernateJpaUtil etc as suggest the Gilead tutorial? – kostas trichas Apr 07 '11 at 16:15
  • you can use that entity manager on the server side, no problems. But for the client side - follow gilead tutorial. – Bozho Apr 07 '11 at 16:19
  • I use RPC to pass it to the client, but it can't desirialize collections with are annotated as @OneToOne relationships. That's why i want to use Gilead, but i can't get the tutorial working. Iam just a student. Thank you for your help anyway :) – kostas trichas Apr 07 '11 at 16:22
  • I will appreciate your answer in this post: http://stackoverflow.com/q/5567444/543544 , that is related to this one. – kostas trichas Apr 07 '11 at 16:28
  • 1
    sorry, but I am no gilead expert. I only know the idea behind it, so the details on how to bootstrap it would be better taken from the tutorial than from me. And I believe Chris's answer is a good one. – Bozho Apr 07 '11 at 16:29
2

To use Gilead with GWT, first change your GWT-RPC service implementations from

public class MyServiceImpl extends RemoteServiceServlet implements MyService {
    ....
}

into:

public class MyServiceImpl extends PersistentRemoteService implements MyService {
    ....
}

Then, in the constructor of these classes, call the method setBeanManager(beanManager). Perform the setup as I described in my other answer. Here's the entire code snippet for reference:

public class MyServiceImpl extends PersistentRemoteService implements MyService {


  public MyServiceImpl() {

    EntityManagerFactory emf = EMF.get();

    HibernateJpaUtil hibernateJpaUtil = new HibernateJpaUtil();
    hibernateJpaUtil.setEntityManagerFactory(emf);

    PersistentBeanManager persistentBeanManager =
      GwtConfigurationHelper.initGwtStatelessBeanManager(hibernateJpaUtil);

    setBeanManager(persistentBeanManager);
  }

  // Service methods follow here

}

This is sufficient for the setup - Gilead then uses the bean manager (and HibernateJpaUtils) automatically under the covers, you don't have to interact directly with it. All you have to do is to make sure, that your entities extend net.sf.gilead.pojo.gwt.LightEntity.

Community
  • 1
  • 1
Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • Please give an example how to persist an entity – kostas trichas Apr 07 '11 at 21:02
  • 1
    @kostas: Persisting an entity works as usual in JPA. See http://docs.jboss.org/hibernate/core/3.6/quickstart/en-US/html/hibernate-gsg-tutorial-jpa.html . (Of course, you'll have to do that on the server side. Implement a GWT-RPC service method, which accepts an entity - or multiple entities - from the client, and then persist as explained.) – Chris Lercher Apr 07 '11 at 22:08