0

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

この投稿 (Gilead JPA configuration)を参照してください。HibernateUtil、HibernateJpaUtil、または PersistentBeanManager などの使用方法をまだ理解できません...

4

2 に答える 2

2

GWTでGileadを使用するには、最初にGWT-RPCサービスの実装を次のように変更します。

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

の中へ:

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

次に、これらのクラスのコンストラクターで、メソッドを呼び出しますsetBeanManager(beanManager)他の回答で説明したようにセットアップを実行します。参考までに、コードスニペット全体を次に示します。

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はBeanManager(およびHibernateJpaUtils)を自動的に内部で使用するため、直接操作する必要はありません。あなたがしなければならないのは、あなたのエンティティが拡張することを確認することnet.sf.gilead.pojo.gwt.LightEntityです。

于 2011-04-07T16:36:10.000 に答える
2

あなたの実装はかなり十分です。ただし、ファクトリを静的にするのではなく、サーブレット コンテキストに配置します。

しかし、ここで重要なことに注意してください。上記のコードは、純粋にサーバー側で使用している場合に機能します。

GWT を使用しているため、(合理的ではないと思いますが) クライアント側で休止状態の「もの」を使用することは可能です。そのためには、前述のユーティリティが必要な gilead が必要です。

于 2011-04-07T16:09:10.297 に答える