我不知道什么是HibernateUtil是...
JPA是否需要?
我将JPA与GWT一起使用,此实现是否足够?
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;
}
}
并在使用时:
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();
}
}
}
请参阅此post (Gilead JPA configuration)。我还不明白,如何使用HibernateUtil或HibernateJpaUtil或PersistentBeanManager的东西...
最佳答案
要将Gilead与GWT一起使用,请先从更改GWT-RPC服务的实现
public class MyServiceImpl extends RemoteServiceServlet implements MyService {
....
}
变成:
public class MyServiceImpl extends PersistentRemoteService implements MyService {
....
}
然后,在这些类的构造函数中,调用方法
setBeanManager(beanManager)
。按照我的other answer中所述执行设置。这是供参考的完整代码段: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
}
这足以进行设置-Gilead然后自动在后台使用Bean管理器(和HibernateJpaUtils),您不必直接与其交互。您要做的就是确保您的实体扩展
net.sf.gilead.pojo.gwt.LightEntity
。