Class JpaHelper


  • public class JpaHelper
    extends java.lang.Object
    This sample illustrates the JPA helper methods that may be of use to EclipseLink customers attempting to leverage EclipseLink specific functionality.
    Author:
    dclarke, gpelletie
    • Constructor Summary

      Constructors 
      Constructor Description
      JpaHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static javax.persistence.EntityManagerFactory createEntityManagerFactory​(java.lang.String sessionName)
      Create a EclipseLink EMF using a session name and sessions.xml.
      static javax.persistence.EntityManagerFactory createEntityManagerFactory​(Server session)
      Create a EclipseLink EMF given a ServerSession that has already been created and logged in.
      static javax.persistence.Query createQuery​(DatabaseQuery query, javax.persistence.EntityManager em)
      Create a EclipseLink JPA query dynamically given a EclipseLink query.
      static DatabaseQuery getDatabaseQuery​(javax.persistence.Query query)
      Access the internal EclipseLink query wrapped within the JPA query.
      static DatabaseSession getDatabaseSession​(javax.persistence.EntityManagerFactory emf)
      Retrieve the shared database session from the EMF.
      static JpaEntityManager getEntityManager​(javax.persistence.EntityManager entityManager)
      Convert a JPA entityManager into a EclipseLink specific one.
      static JpaEntityManagerFactory getEntityManagerFactory​(javax.persistence.EntityManager em)
      Given an EntityManager return the EntityManagerFactory that created it.
      static JpaEntityManagerFactory getEntityManagerFactory​(javax.persistence.EntityManagerFactory emf)
      Given a JPA EntityManagerFactory attempt to cast it to a EclipseLink EMF.
      static org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl getEntityManagerFactory​(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl emf)
      Deprecated. 
      static ReadAllQuery getReadAllQuery​(javax.persistence.Query query)
      Access the internal EclipseLink query wrapped within the JPA query.
      static ReportQuery getReportQuery​(javax.persistence.Query query)
      Access the internal EclipseLink query wrapped within the JPA query.
      static Server getServerSession​(javax.persistence.EntityManagerFactory emf)
      Retrieve the shared server session from the EMF.
      static SessionBroker getSessionBroker​(javax.persistence.EntityManagerFactory emf)
      Retrieve the shared session broker from the EMF.
      static boolean isEclipseLink​(javax.persistence.EntityManager em)
      Verify if the JPA provider is EclipseLink.
      static boolean isEclipseLink​(javax.persistence.EntityManagerFactory emf)
      Verify if the JPA provider is EclipseLink
      static boolean isEclipseLink​(javax.persistence.Query query)
      Verify if the JPA provider is EclipseLink
      static boolean isReportQuery​(javax.persistence.Query query)
      Determine if the JPA query is a EclipseLink ReportQuery.
      static void loadUnfetchedObject​(FetchGroupTracker entity)
      If the object has a fetch group then the whole object is read in.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JpaHelper

        public JpaHelper()
    • Method Detail

      • isEclipseLink

        public static boolean isEclipseLink​(javax.persistence.EntityManager em)
        Verify if the JPA provider is EclipseLink. If you are in a container and not in a transaction this method may incorrectly return false. It is always more reliable to check isEclipseLink on the EMF or Query.
      • isEclipseLink

        public static boolean isEclipseLink​(javax.persistence.EntityManagerFactory emf)
        Verify if the JPA provider is EclipseLink
      • isEclipseLink

        public static boolean isEclipseLink​(javax.persistence.Query query)
        Verify if the JPA provider is EclipseLink
      • isReportQuery

        public static boolean isReportQuery​(javax.persistence.Query query)
        Determine if the JPA query is a EclipseLink ReportQuery. Useful for frameworks that want to determine which get_X_Query method they can safely invoke.
      • getReportQuery

        public static ReportQuery getReportQuery​(javax.persistence.Query query)
        Access the internal EclipseLink query wrapped within the JPA query. A EclipseLink JPA created from JP QL contains a ReportQuery if multiple items or a non-entity type is being returned. This method will fail if a single entity type is being returned as the query is a ReadAllQuery.
        See Also:
        getReadAllQuery(javax.persistence.Query)
      • getDatabaseQuery

        public static DatabaseQuery getDatabaseQuery​(javax.persistence.Query query)
        Access the internal EclipseLink query wrapped within the JPA query.
      • getReadAllQuery

        public static ReadAllQuery getReadAllQuery​(javax.persistence.Query query)
        Access the internal EclipseLink query wrapped within the JPA query. A EclipseLink JPA created from JP QL only contains a ReadAllQuery if only a single entity type is being returned. A ReadAllQuery is the super class of a ReportQuery so this method will always work for either a ReportQuery or ReadAllQuery.
      • createQuery

        public static javax.persistence.Query createQuery​(DatabaseQuery query,
                                                          javax.persistence.EntityManager em)
        Create a EclipseLink JPA query dynamically given a EclipseLink query.
      • getEntityManager

        public static JpaEntityManager getEntityManager​(javax.persistence.EntityManager entityManager)
        Convert a JPA entityManager into a EclipseLink specific one. This will work both within a JavaSE deployment as well as within a container where the EntityManager may be wrapped. In the case where the container is not in a transaction it may return null for its delegate. When this happens the only way to access an EntityManager is to use the EntityManagerFactory to create a temporary one where the application manage its lifecycle.
      • getEntityManagerFactory

        public static org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl getEntityManagerFactory​(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl emf)
        Deprecated.
        Given a JPA EntityManagerFactory attempt to cast it to a EclipseLink EMF. Although this method currently returns an instance of EntityManagerFactoryImpl, it is recommended that users cast to JpaEntityManagerFactory. Future versions of EclipseLink will return that interface from this method instead
        See Also:
        JpaEntityManagerFactory
      • getEntityManagerFactory

        public static JpaEntityManagerFactory getEntityManagerFactory​(javax.persistence.EntityManagerFactory emf)
        Given a JPA EntityManagerFactory attempt to cast it to a EclipseLink EMF.
        See Also:
        JpaEntityManagerFactory
      • getEntityManagerFactory

        public static JpaEntityManagerFactory getEntityManagerFactory​(javax.persistence.EntityManager em)
        Given an EntityManager return the EntityManagerFactory that created it. This method must be called on an open entity manager and will return null if called on a closed entityManager. This method will return null for non-EclipseLink EntityManagers.
        See Also:
        JpaEntityManagerFactory
      • getDatabaseSession

        public static DatabaseSession getDatabaseSession​(javax.persistence.EntityManagerFactory emf)
        Retrieve the shared database session from the EMF.
      • getServerSession

        public static Server getServerSession​(javax.persistence.EntityManagerFactory emf)
        Retrieve the shared server session from the EMF.
      • getSessionBroker

        public static SessionBroker getSessionBroker​(javax.persistence.EntityManagerFactory emf)
        Retrieve the shared session broker from the EMF.
      • createEntityManagerFactory

        public static javax.persistence.EntityManagerFactory createEntityManagerFactory​(Server session)
        Create a EclipseLink EMF given a ServerSession that has already been created and logged in.
      • createEntityManagerFactory

        public static javax.persistence.EntityManagerFactory createEntityManagerFactory​(java.lang.String sessionName)
        Create a EclipseLink EMF using a session name and sessions.xml. This is equivalent to using the EclipseLink.session-xml and EclipseLink.session-name PU properties with the exception that no persistence.xml is required. The application would be required to manage this singleton EMF.
      • loadUnfetchedObject

        public static void loadUnfetchedObject​(FetchGroupTracker entity)
        If the object has a fetch group then the whole object is read in.