回答:
とを優先EntityManagerFactoryしEntityManagerます。これらはJPA標準で定義されています。
SessionFactoryそして、Session休止状態固有のものです。はEntityManager内部で休止状態セッションを呼び出します。また、で利用できない特定の機能が必要な場合はEntityManager、次を呼び出してセッションを取得できます。
Session session = entityManager.unwrap(Session.class);
SessionからではEntityManager同じ、SessionFactory.getCurrentSession()?Sessionつまり、まだ作成されていない場合、新しく開きますか?マルチスレッド環境ではどのように機能しますか?
これに加えて、getDelegate()メソッドをから呼び出してHibernateのセッションを取得することもできますEntityManager。
例:
Session session = (Session) entityManager.getDelegate();
JPA2 EntityManagerAPIのSessionFactory方がモダンな感じがするので、私はJPA2 APIよりも好みます。簡単な例:
JPA:
@PersistenceContext
EntityManager entityManager;
public List<MyEntity> findSomeApples() {
return entityManager
.createQuery("from MyEntity where apples=7", MyEntity.class)
.getResultList();
}
SessionFactory:
@Autowired
SessionFactory sessionFactory;
public List<MyEntity> findSomeApples() {
Session session = sessionFactory.getCurrentSession();
List<?> result = session.createQuery("from MyEntity where apples=7")
.list();
@SuppressWarnings("unchecked")
List<MyEntity> resultCasted = (List<MyEntity>) result;
return resultCasted;
}
EntityManagerは簡単にモックできるので、最初のものは見た目がきれいで、テストも簡単であることは明らかだと思います。
return sessionFactory.getCurrentSession().createQuery("from User where id=1").list()
SessionFactory 対 EntityManagerFactory私が説明したようにHibernateユーザーガイド、HibernateはSessionFactoryJPAを拡張しEntityManagerFactory、次の図に示すように、:
したがって、SessionFactoryもJPA EntityManagerFactoryです。
どちらSessionFactoryとEntityManagerFactoryエンティティのマッピング・メタデータが含まれていると、あなたが休止状態を作成することができますSessionかEntityManager。
Session 対 EntityManager同じようにSessionFactoryとEntityManagerFactory、HibernateはSessionJPAを拡張しますEntityManager。したがって、によって定義されたすべてのメソッドEntityManagerはHibernateで使用できますSession。
Sessionそして`のEntityManagerが翻訳したエンティティの状態遷移を SELECT、INSERT、UPDATEのように、SQL文の中に、およびDELETE。
JPAまたはHibernateアプリケーションをブートストラップする場合、2つの選択肢があります。
SessionFactory経由でBootstrapServiceRegistryBuilder。Springを使用している場合LocalSessionFactoryBean、このGitHubの例に示すように、Hibernateブートストラップはを介して行われます。EntityManagerFactoryを介してJPA を作成できます。Springを使用している場合、このGitHubの例で示されているように、JPAブートストラップはを介して行われます。PersistenceEntityManagerFactoryBuilderLocalContainerEntityManagerFactoryBeanJPAによるブートストラップが推奨されます。だとJPAがあるためFlushModeType.AUTO、従来よりもはるかに良い選択であるFlushMode.AUTO、休憩を読み取り、あなたの-書き込み、ネイティブSQLクエリの一貫性が。
また、JPAを介してブートストラップEntityManagerFactoryし、@PersistenceUnitアノテーションを介してを挿入した場合:
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
メソッドSessionfactoryを使用して、基になるものに簡単にアクセスできますunwrap。
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
JPAでも同じことができますEntityManager。アノテーションEntityManagerを介してを注入する場合@PersistenceContext:
@PersistenceContext
private EntityManager entityManager;
メソッドSessionを使用して、基になるものに簡単にアクセスできますunwrap。
Session session = entityManager.unwrap(Session.class);
したがって、JPA を介してエンティティをフェッチするなど、JPAで利用できない一部のHibernate固有のメソッドにアクセスする場合はEntityManagerFactory、JPAを介してブートストラップし、およびを使用してEntityManager、それらを関連するHibernateインターフェースにアンラップする必要があります。
EntityManagerを使用することにより、コードは休止状態と密接に結合されなくなりました。しかし、このためには、使用法で使用する必要があります:
javax.persistence.EntityManager
の代わりに
org.hibernate.ejb.HibernateEntityManager
同様に、EntityManagerFactoryにはjavaxインターフェースを使用します。このようにして、コードは疎結合されます。hibernateよりも優れたJPA 2実装がある場合、切り替えは簡単です。極端な場合は、HibernateEntityManagerに型キャストできます。
EntityManagerインターフェースは、HibernateのsessionFactoryに似ています。javax.persistanceパッケージのEntityManagerが、org.hibernate.Session / sessionFactoryパッケージのsessionおよびsessionFactory。
エンティティーマネージャーはJPA固有であり、セッション/セッションファクトリーは休止状態固有です。