com.mizar.persistence.jpa.utils
Class JPAUtils

java.lang.Object
  extended by com.mizar.persistence.jpa.utils.JPAUtils

public class JPAUtils
extends java.lang.Object


Constructor Summary
JPAUtils()
           
 
Method Summary
static javax.persistence.EntityManager closeEm(javax.persistence.EntityManager em)
          If em is not null and is open then it is closed.
static void closePersistenceUnit(javax.persistence.EntityManagerFactory emf, javax.persistence.EntityManager em)
           
static boolean containsErrorCode(java.lang.Exception exception, int code)
          The method recursively checks exception and its causes for SQLExceptions with a matching code
static boolean containsErrorCode(java.lang.Exception exception, int[] codes)
          The method recursively checks exception and its causes for SQLExceptions with a matching an element of codes
static java.lang.Object convertPrimaryKeyFromStringToObject(java.lang.Class beanClass, java.lang.String pkey)
          We often get a primary key as a String value, particularly when it originates from Javascript.
static java.lang.Long createSequence(javax.persistence.EntityManager em, java.lang.String sequenceName)
           
static java.lang.Long createSequence(javax.persistence.EntityManager em, java.lang.String sequenceName, java.lang.Long startWith)
           
static java.lang.Long createSequence(javax.persistence.EntityManager em, java.lang.String sequenceName, java.lang.Long startWith, java.lang.Long minValue, java.lang.Long maxValue, java.lang.Long increment, java.lang.Long cacheSize, java.lang.Boolean cycle, java.lang.Boolean order)
           
static java.lang.Long createSequence(javax.persistence.EntityManager em, java.lang.String sequenceName, java.lang.String tableName, java.lang.String columnName)
           
static java.lang.Object detach(javax.persistence.EntityManager em, java.lang.Object entity)
          Detach an entity bean from the persistence context
static boolean dropIndex(javax.persistence.EntityManager em, java.lang.String indexName)
           
static boolean dropSequence(javax.persistence.EntityManager em, java.lang.String sequenceName)
           
static boolean dropTable(javax.persistence.EntityManager em, java.lang.String tableName, boolean purge)
           
static boolean dropTablespace(javax.persistence.EntityManager em, java.lang.String tableName, boolean includingContents, boolean includingDatafiles)
           
static java.lang.String findAll(java.lang.Class entityClass)
           
static java.lang.Object findFreshBean(javax.persistence.EntityManager em, java.lang.Class beanClass, java.lang.Object pkeyValue)
          This method executes a EntityManager.find followed by an EntityManager.refesh
static java.lang.Object findFreshBean(javax.persistence.EntityManager em, java.lang.Class beanClass, java.lang.String pkey)
           
static java.util.List<java.lang.Class> getBeanClasses(javax.persistence.EntityManager em)
           
static java.lang.String getColumnName(javax.persistence.EntityManager em, java.lang.Class beanClass, java.lang.String propertyName)
           
static java.lang.String getColumnNames(javax.persistence.EntityManager em, java.lang.Class beanClass, java.lang.String alias)
           
static java.sql.Connection getConnection(javax.persistence.EntityManager em)
          STILL DOES NOT WORK -- but we'll leave here as a future project to sort out
static java.lang.String getConnectionPoolName(javax.persistence.EntityManager em)
          THIS DOES NOT APPEAR TO WORK AS EXPECTED -- but we'll leave here as a future project to sort out
static java.util.List<java.lang.String> getDataSource(javax.servlet.ServletContext context, java.lang.String persistenceUnitName)
          This method opens the WEB-INF/classes/META-INF/persistence.xml and returns a List of all non-jta-data-source and jta-data-source values.
static java.util.HashMap<java.lang.String,java.lang.Object> getDefinedBeanValues(javax.persistence.EntityManager em, java.lang.Object entityBean)
          This method currently only returns none Collection properties.
static java.lang.Class getEntityClass(java.lang.Class subclass)
          The method loops through super classes of subclass until it finds a parent class that is a JPA Entity Bean.
static java.lang.Class getEntityClass(javax.persistence.EntityManager em, java.lang.String tableName)
           
static java.util.List<java.lang.Class> getEntityClassesByPackage(javax.persistence.EntityManager em, java.lang.String packageName)
          The method returns all classes known to the EntityManager that are members of the package and contain an @Entity annotation
static javax.persistence.EntityManager getEntityManager(javax.persistence.EntityManagerFactory emf)
           
static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName)
           
static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName, java.util.HashMap emProperties)
           
static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName, java.util.HashMap emProperties, JdbcParameters jdbcParameters)
          This method is EXPERIMENTAL.
static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName, java.util.HashMap emProperties, JdbcParameters jdbcParameters, java.lang.String parameterPrefix)
          This method is EXPERIMENTAL.
static java.lang.Object getEntityObject(java.lang.Object entityImplementor)
          This method will manufactor an instance of the underlying entity class.
static java.sql.SQLException getFirstSQLException(java.lang.Exception exception)
           
static java.lang.Long getLargestPrimaryKeyValue(javax.persistence.EntityManager em, java.lang.Class entityClass)
           
static java.lang.Class getMappedSuperClass(java.lang.Class subclass)
          The method loops through super classes of subclass until it finds a parent class that is a JPA Entity Bean.
static java.lang.Long getNextSequence(javax.persistence.EntityManager em, java.lang.String sequenceName)
           
static java.lang.reflect.Method getPrimaryKeyGetter(java.lang.Class entityClass)
          The method first uses getEntityClass to find the JPA Entity for entityClass.
static java.lang.String getPrimaryKeyName(java.lang.Class entityClass)
           
static java.lang.String getPrimaryKeyName(javax.persistence.EntityManager em, java.lang.Class beanClass)
           
static java.lang.Class getPrimaryKeyType(java.lang.Class entityClass)
           
static java.lang.Class getPrimaryKeyType(javax.persistence.EntityManager em, java.lang.Class entityClass)
           
static java.lang.Object getPrimaryKeyValue(java.lang.Object entityBean)
           
static java.util.Map getProperties(javax.persistence.EntityManager em)
          We use this method to get com.mizar.persistence properties from persistence.xml or other properties that might be added when the EntityManagerFactory is created.
static java.util.Map getProperties(javax.persistence.EntityManagerFactory em)
           
static java.util.Map<java.lang.String,java.lang.String> getProperties(javax.persistence.EntityManager em, java.lang.String packagebase)
           
static java.lang.String getProperty(javax.persistence.EntityManagerFactory emf, java.lang.String propertyName)
           
static java.lang.String getProperty(javax.persistence.EntityManager em, java.lang.String name)
           
static java.lang.String getPropertyName(javax.persistence.EntityManager em, java.lang.Class beanClass, java.lang.String columnName)
           
static java.util.List<java.lang.Class> getReferencedBeans(javax.persistence.EntityManager em, java.lang.Class entityClass)
          This method is useful for determining other entity beans referenced by an entityBean class.
static java.util.Set<java.lang.Class> getReferencedBeans(java.lang.Object bean)
           
static javax.persistence.SequenceGenerator getSequenceGenerator(java.lang.Class entityClass)
           
static javax.persistence.SequenceGenerator getSequenceGenerator(java.lang.reflect.Field field)
           
static java.lang.String getTableName(javax.persistence.EntityManager em, java.lang.Class beanClass)
           
static java.util.List<java.lang.String> getTableNames(javax.persistence.EntityManager em)
           
static java.util.Map<java.lang.String,UserSequencesBean> getUserSequences(javax.persistence.EntityManager em)
           
static java.lang.Class isAnnotationPresent(java.lang.Class entityClass, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
           
static boolean isSQLExceptionErrorCode(java.lang.Exception exception, int errorCode)
          The method recursively tests the exception and all causes looking for an SQLException with the given errorCode
static boolean isSQLExceptionErrorCode(java.lang.Exception exception, int[] errorCodes)
           
static boolean isSynonym(javax.persistence.EntityManager em, java.lang.String tableName)
           
static java.util.List<java.lang.String> listPersistenceUnits(javax.servlet.ServletContext context)
           
static java.lang.Object merge(javax.persistence.EntityManager em, java.lang.Object entityImplementor)
           
static void nativeQueryTester(javax.persistence.EntityManager em, java.lang.String sql, int max, java.lang.Class returnClass)
           
static void printAnyBean(java.lang.Object result)
          Used by queryTester()
static void queryTester(javax.persistence.EntityManager em, java.lang.String jpql, int max)
          The code for this method was taken from Pro EJB 3: Java Persistence API by Keith and Schincariol published by apress 2006, pages 195-196.
static java.util.HashMap<java.lang.String,java.lang.String> readProperties(javax.servlet.ServletContext context, java.lang.String persistenceUnitName)
           
static java.util.HashMap<java.lang.String,java.lang.String> readProperties(javax.servlet.ServletContext context, java.lang.String persistenceUnitName, java.lang.String packageName)
           
static GenericBean refreshEntityObject(javax.persistence.EntityManager em, java.lang.Object entityImplementor)
          The method uses the EntityManager to find the instance of the entity class underlying the implementor and uses ReflectUtils.castToSuperclass to copy over any updated values.
static GenericBean refreshEntityObject(javax.persistence.EntityManager em, java.lang.Object entityImplementor, java.lang.Object primaryKey)
           
static void removeDuplicateEntries(java.util.List<GenericBean> list)
          The method removes redundant beans from the list.
static java.lang.Object selectFreshBean(javax.persistence.EntityManager em, java.lang.Class beanClass, java.lang.String pkey)
          This method SHOULD result in exactly the same results as findFreshBean(EntityManager, Class, String) but instead of using em.find(beanClass,pkey) it constructs a JPQL select statement using a hint.
static void testEntityClasses(javax.persistence.EntityManager em, java.lang.String packageName, int maxRows)
           
static void testEntityClasses(javax.persistence.EntityManager em, java.lang.String packageName, int maxRows, java.util.List<java.lang.String> ignoreClasses)
           
static boolean userSequenceExists(javax.persistence.EntityManager em, java.lang.String sequenceName)
           
static void validateSequences(javax.persistence.EntityManager em)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JPAUtils

public JPAUtils()
Method Detail

getEntityManagerFactory

public static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName)

getEntityManagerFactory

public static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName,
                                                                             java.util.HashMap emProperties)
Returns:

getEntityManagerFactory

public static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName,
                                                                             java.util.HashMap emProperties,
                                                                             JdbcParameters jdbcParameters)
This method is EXPERIMENTAL. It has limited application as we do not seem to be able to change the connection information once we have connected even once with a given persistenceUnit. This is true even if we close the EntityManagerFactory and open a new one.

Parameters:
persistenceUnitName -
emProperties -
jdbcParameters - These will only be valid the first time an application is run. After than they are ignored.
Returns:

getEntityManagerFactory

public static javax.persistence.EntityManagerFactory getEntityManagerFactory(java.lang.String persistenceUnitName,
                                                                             java.util.HashMap emProperties,
                                                                             JdbcParameters jdbcParameters,
                                                                             java.lang.String parameterPrefix)
This method is EXPERIMENTAL. It has limited application as we do not seem to be able to change the connection information once we have connected even once with a given persistenceUnit. This is true even if we close the EntityManagerFactory and open a new one.

Parameters:
persistenceUnitName -
emProperties -
jdbcParameters - These will only be valid the first time an application is run. After than they are ignored.
parameterPrefix - either eclipselink.jdbc or persistence.javax.jdbc
Returns:

getEntityManager

public static javax.persistence.EntityManager getEntityManager(javax.persistence.EntityManagerFactory emf)
Returns:

closeEm

public static javax.persistence.EntityManager closeEm(javax.persistence.EntityManager em)
If em is not null and is open then it is closed. em is set to null.


closePersistenceUnit

public static void closePersistenceUnit(javax.persistence.EntityManagerFactory emf,
                                        javax.persistence.EntityManager em)

detach

public static java.lang.Object detach(javax.persistence.EntityManager em,
                                      java.lang.Object entity)
Detach an entity bean from the persistence context

Parameters:
em -
entity - entity to be detached
Returns:
a detached entity

isAnnotationPresent

public static java.lang.Class isAnnotationPresent(java.lang.Class entityClass,
                                                  java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)

getDataSource

public static java.util.List<java.lang.String> getDataSource(javax.servlet.ServletContext context,
                                                             java.lang.String persistenceUnitName)
                                                      throws org.jdom.JDOMException,
                                                             java.io.IOException
This method opens the WEB-INF/classes/META-INF/persistence.xml and returns a List of all non-jta-data-source and jta-data-source values. The non-jta-data-source values preceed any jta-data-source values.

Parameters:
context -
persistenceUnitName -
Returns:
a List of datasource names that were found within the persistence-unit
Throws:
org.jdom.JDOMException
java.io.IOException

listPersistenceUnits

public static java.util.List<java.lang.String> listPersistenceUnits(javax.servlet.ServletContext context)
Parameters:
context -
Returns:

readProperties

public static java.util.HashMap<java.lang.String,java.lang.String> readProperties(javax.servlet.ServletContext context,
                                                                                  java.lang.String persistenceUnitName)
                                                                           throws org.jdom.JDOMException,
                                                                                  java.io.IOException
Throws:
org.jdom.JDOMException
java.io.IOException

readProperties

public static java.util.HashMap<java.lang.String,java.lang.String> readProperties(javax.servlet.ServletContext context,
                                                                                  java.lang.String persistenceUnitName,
                                                                                  java.lang.String packageName)
                                                                           throws org.jdom.JDOMException,
                                                                                  java.io.IOException
Throws:
org.jdom.JDOMException
java.io.IOException

getProperties

public static java.util.Map<java.lang.String,java.lang.String> getProperties(javax.persistence.EntityManager em,
                                                                             java.lang.String packagebase)

getProperties

public static java.util.Map getProperties(javax.persistence.EntityManager em)
We use this method to get com.mizar.persistence properties from persistence.xml or other properties that might be added when the EntityManagerFactory is created.

Parameters:
em - an open EntityManager
Returns:
the property map for the EntityManager

getProperty

public static java.lang.String getProperty(javax.persistence.EntityManager em,
                                           java.lang.String name)
Parameters:
em - an open EntityManager
name - the name of a property to get the value for
Returns:
the value of the property name or null if not found.

getProperties

public static java.util.Map getProperties(javax.persistence.EntityManagerFactory em)
Parameters:
em - an open EntityManager
Returns:
a Map of the properties in the open Entity Manager containing name and value

getProperty

public static java.lang.String getProperty(javax.persistence.EntityManagerFactory emf,
                                           java.lang.String propertyName)
Parameters:
emf - an Open EntityManagerFactory
propertyName - the name of the property to look for
Returns:
the value of propertyName

dropIndex

public static boolean dropIndex(javax.persistence.EntityManager em,
                                java.lang.String indexName)

dropTable

public static boolean dropTable(javax.persistence.EntityManager em,
                                java.lang.String tableName,
                                boolean purge)

dropTablespace

public static boolean dropTablespace(javax.persistence.EntityManager em,
                                     java.lang.String tableName,
                                     boolean includingContents,
                                     boolean includingDatafiles)

dropSequence

public static boolean dropSequence(javax.persistence.EntityManager em,
                                   java.lang.String sequenceName)

createSequence

public static java.lang.Long createSequence(javax.persistence.EntityManager em,
                                            java.lang.String sequenceName,
                                            java.lang.Long startWith,
                                            java.lang.Long minValue,
                                            java.lang.Long maxValue,
                                            java.lang.Long increment,
                                            java.lang.Long cacheSize,
                                            java.lang.Boolean cycle,
                                            java.lang.Boolean order)

createSequence

public static java.lang.Long createSequence(javax.persistence.EntityManager em,
                                            java.lang.String sequenceName,
                                            java.lang.Long startWith)

createSequence

public static java.lang.Long createSequence(javax.persistence.EntityManager em,
                                            java.lang.String sequenceName)

createSequence

public static java.lang.Long createSequence(javax.persistence.EntityManager em,
                                            java.lang.String sequenceName,
                                            java.lang.String tableName,
                                            java.lang.String columnName)

getNextSequence

public static java.lang.Long getNextSequence(javax.persistence.EntityManager em,
                                             java.lang.String sequenceName)

userSequenceExists

public static boolean userSequenceExists(javax.persistence.EntityManager em,
                                         java.lang.String sequenceName)

getUserSequences

public static java.util.Map<java.lang.String,UserSequencesBean> getUserSequences(javax.persistence.EntityManager em)

getLargestPrimaryKeyValue

public static java.lang.Long getLargestPrimaryKeyValue(javax.persistence.EntityManager em,
                                                       java.lang.Class entityClass)

isSynonym

public static boolean isSynonym(javax.persistence.EntityManager em,
                                java.lang.String tableName)

getSequenceGenerator

public static javax.persistence.SequenceGenerator getSequenceGenerator(java.lang.Class entityClass)

getSequenceGenerator

public static javax.persistence.SequenceGenerator getSequenceGenerator(java.lang.reflect.Field field)

validateSequences

public static void validateSequences(javax.persistence.EntityManager em)

getEntityClass

public static java.lang.Class getEntityClass(javax.persistence.EntityManager em,
                                             java.lang.String tableName)

getBeanClasses

public static java.util.List<java.lang.Class> getBeanClasses(javax.persistence.EntityManager em)

getTableNames

public static java.util.List<java.lang.String> getTableNames(javax.persistence.EntityManager em)

getTableName

public static java.lang.String getTableName(javax.persistence.EntityManager em,
                                            java.lang.Class beanClass)
Parameters:
em - an open EntityManager
beanClass - a class that either is a JPA @Entity class or that implements one
Returns:
the name of the database table associated with beanClass
See Also:
getEntityClass(Class)

getPrimaryKeyName

public static java.lang.String getPrimaryKeyName(javax.persistence.EntityManager em,
                                                 java.lang.Class beanClass)
Parameters:
em - an open EntityManager
beanClass - a class that either is a JPA @Entity class or that implements one
Returns:
the name of the database table associated with beanClass
See Also:
getEntityClass(Class)

getColumnName

public static java.lang.String getColumnName(javax.persistence.EntityManager em,
                                             java.lang.Class beanClass,
                                             java.lang.String propertyName)
Parameters:
em - an open EntityManager
beanClass - a class that either is a JPA @Entity class or that implements one
propertyName - the name of a property of beanClass
Returns:
the name of the database column associated with beanClass.propertyName
See Also:
getEntityClass(Class)

getPropertyName

public static java.lang.String getPropertyName(javax.persistence.EntityManager em,
                                               java.lang.Class beanClass,
                                               java.lang.String columnName)
Parameters:
em - an open EntityManager
beanClass - a class that either is a JPA @Entity class or that implements one
columnName -
Returns:
the property name associated with columnName, null if not found.

getColumnNames

public static java.lang.String getColumnNames(javax.persistence.EntityManager em,
                                              java.lang.Class beanClass,
                                              java.lang.String alias)
Parameters:
em - an open EntityManager
beanClass - a class that either is a JPA @Entity class or that implements one
alias - an alias to preface the column names with
Returns:
alias.column1, alias.column2, ..., alias.column-n
See Also:
getEntityClass(Class)

refreshEntityObject

public static GenericBean refreshEntityObject(javax.persistence.EntityManager em,
                                              java.lang.Object entityImplementor)
The method uses the EntityManager to find the instance of the entity class underlying the implementor and uses ReflectUtils.castToSuperclass to copy over any updated values.

Parameters:
em - an open EntityManager
entityImplementor - an instance of a class the extends GenericBean
Returns:
the cached instance of the entity super class of entityImplementor updated with the value of entityImplementor
See Also:
getEntityClass(Class), ReflectUtils.castToSuperclass(Object,Object)

refreshEntityObject

public static GenericBean refreshEntityObject(javax.persistence.EntityManager em,
                                              java.lang.Object entityImplementor,
                                              java.lang.Object primaryKey)

merge

public static java.lang.Object merge(javax.persistence.EntityManager em,
                                     java.lang.Object entityImplementor)

getEntityObject

public static java.lang.Object getEntityObject(java.lang.Object entityImplementor)
This method will manufactor an instance of the underlying entity class. Unlike JPAUtils.refreshEntityObject this method does not look in the cache for any existing instance of the entity class. If an EntityManager is available JPAUtils.refreshEntityObject should be used in preference over this method.

Parameters:
entityImplementor - an instance of a class the extends GenericBean
Returns:
the underlying @Entity superclass of entityImplementor
See Also:
getEntityClass(Class), ReflectUtils.castToSuperclass(Object,Object), refreshEntityObject(javax.persistence.EntityManager, java.lang.Object)

getEntityClass

public static java.lang.Class getEntityClass(java.lang.Class subclass)
The method loops through super classes of subclass until it finds a parent class that is a JPA Entity Bean.

Parameters:
subclass - a class that contains a @Entity annotation or is a subclass of such a class
Returns:
the first class in the parent hierachy of subclass that contains a @Entity annotation.
See Also:
ReflectUtils.getAnnotatedSuperClass(Class,Class)

getMappedSuperClass

public static java.lang.Class getMappedSuperClass(java.lang.Class subclass)
The method loops through super classes of subclass until it finds a parent class that is a JPA Entity Bean.

Parameters:
subclass - a class that contains a @MappedSuperClass annotation or is a subclass of such a class
Returns:
the first class in the parent hierachy of subclass that contains a @MappedSuperClass annotation.
See Also:
ReflectUtils.getAnnotatedSuperClass(Class,Class)

getPrimaryKeyGetter

public static java.lang.reflect.Method getPrimaryKeyGetter(java.lang.Class entityClass)
                                                    throws java.lang.NoSuchMethodException

The method first uses getEntityClass to find the JPA Entity for entityClass. It then looks for a instance of @IdClass, and not finding one it then iterates over all the Field definitions looking for a Field that is annotated with either an @Id or an @EmbeddedId annotation. If it finds one it returns the getter for the Field. If it does not find one, then it iterates over all the Methods looking for a Method with either an @Id or an @EmbeddedId annotation.

The @EmbeddedId pattern have been tested as of 2008-08-26 m.millman

The @IdClass pattern is not coded as of 2008-08-26 m.millman

Parameters:
entityClass - a JPA Entity Bean class or a subclass of one.
Returns:
the primary key getter for entityClass
Throws:
java.lang.NoSuchMethodException
See Also:
ReflectUtils.getGetter(Class clazz,String fieldName)

getPrimaryKeyType

public static java.lang.Class getPrimaryKeyType(javax.persistence.EntityManager em,
                                                java.lang.Class entityClass)

getPrimaryKeyType

public static java.lang.Class getPrimaryKeyType(java.lang.Class entityClass)
                                         throws java.lang.NoSuchMethodException
Throws:
java.lang.NoSuchMethodException

getPrimaryKeyName

public static java.lang.String getPrimaryKeyName(java.lang.Class entityClass)
                                          throws java.lang.NoSuchMethodException
Throws:
java.lang.NoSuchMethodException

convertPrimaryKeyFromStringToObject

public static java.lang.Object convertPrimaryKeyFromStringToObject(java.lang.Class beanClass,
                                                                   java.lang.String pkey)
                                                            throws java.lang.NoSuchMethodException

We often get a primary key as a String value, particularly when it originates from Javascript. This method will convert the String value to the correct object name. Supported Objects include:

Parameters:
beanClass - a JPA Entity Bean class
pkey - a primary key
Returns:
the value of the primary key as an Object that meets the name required for EntityManager.find
Throws:
java.lang.NoSuchMethodException

selectFreshBean

public static java.lang.Object selectFreshBean(javax.persistence.EntityManager em,
                                               java.lang.Class beanClass,
                                               java.lang.String pkey)
                                        throws java.lang.NoSuchMethodException
This method SHOULD result in exactly the same results as findFreshBean(EntityManager, Class, String) but instead of using em.find(beanClass,pkey) it constructs a JPQL select statement using a hint.
Query query = em.createQuery(jpql).setHint("eclipselink.refresh","true");

Parameters:
em - an open EntityManager
beanClass - the class of JPA Entity bean to find
pkey - the value of the primary key to find
Returns:
an instance of beanClass
Throws:
java.lang.NoSuchMethodException
See Also:
findFreshBean(EntityManager,Class,String)

findFreshBean

public static java.lang.Object findFreshBean(javax.persistence.EntityManager em,
                                             java.lang.Class beanClass,
                                             java.lang.String pkey)
                                      throws java.lang.NoSuchMethodException
Parameters:
em - an open EntityManager
beanClass - the class of JPA Entity bean to find
pkey - the value of the primary key to find, as a String
Returns:
an instance of beanClass
Throws:
java.lang.NoSuchMethodException
See Also:
findFreshBean(EntityManager,Class,Object)

findFreshBean

public static java.lang.Object findFreshBean(javax.persistence.EntityManager em,
                                             java.lang.Class beanClass,
                                             java.lang.Object pkeyValue)
                                      throws java.lang.NoSuchMethodException
This method executes a EntityManager.find followed by an EntityManager.refesh

Parameters:
em - an open EntityManager
beanClass - the class of JPA Entity bean to find
pkeyValue - the value of the primary key, it must match the type of the beanClass primary key
Returns:
an instance of beanClass
Throws:
java.lang.NoSuchMethodException
See Also:
findFreshBean(EntityManager,Class,String)

getPrimaryKeyValue

public static java.lang.Object getPrimaryKeyValue(java.lang.Object entityBean)

getReferencedBeans

public static java.util.List<java.lang.Class> getReferencedBeans(javax.persistence.EntityManager em,
                                                                 java.lang.Class entityClass)
This method is useful for determining other entity beans referenced by an entityBean class. It is used in AbstractCollection to cross reference SessionConstants.getBeanTableMap

Parameters:
em - an open EntityManager
entityClass - a JPA Entity class
Returns:
a list of classes known to em referenced by entityClass

getReferencedBeans

public static java.util.Set<java.lang.Class> getReferencedBeans(java.lang.Object bean)

getConnection

public static java.sql.Connection getConnection(javax.persistence.EntityManager em)
STILL DOES NOT WORK -- but we'll leave here as a future project to sort out

DO NOT CLOSE THIS CONNECTION as you will surely screw up anyone else using the EntityManager

Parameters:
em - an open EntityManager
Returns:
a connection to the datasource associated with the default connection pool of the EntityManager

getConnectionPoolName

public static java.lang.String getConnectionPoolName(javax.persistence.EntityManager em)
THIS DOES NOT APPEAR TO WORK AS EXPECTED -- but we'll leave here as a future project to sort out

Parameters:
em -
Returns:

getDefinedBeanValues

public static java.util.HashMap<java.lang.String,java.lang.Object> getDefinedBeanValues(javax.persistence.EntityManager em,
                                                                                        java.lang.Object entityBean)
This method currently only returns none Collection properties. Properters that have a null or empty value are not returned.

Parameters:
em - an open EntityManager
entityBean - an entity bean that em knows about
Returns:
a HashMap of property names and values

removeDuplicateEntries

public static void removeDuplicateEntries(java.util.List<GenericBean> list)
The method removes redundant beans from the list.

Parameters:
list -

testEntityClasses

public static void testEntityClasses(javax.persistence.EntityManager em,
                                     java.lang.String packageName,
                                     int maxRows)
Parameters:
em - an open EntityManager
packageName - a package name to filter entity classes from
maxRows - the maximum number of rows to find in each entityClass

testEntityClasses

public static void testEntityClasses(javax.persistence.EntityManager em,
                                     java.lang.String packageName,
                                     int maxRows,
                                     java.util.List<java.lang.String> ignoreClasses)
Parameters:
em - an open EntityManager
packageName - a package name to filter entity classes from
maxRows - the maximum number of rows to find in each entityClass
ignoreClasses - a List of simple class names to ignore

getEntityClassesByPackage

public static java.util.List<java.lang.Class> getEntityClassesByPackage(javax.persistence.EntityManager em,
                                                                        java.lang.String packageName)
The method returns all classes known to the EntityManager that are members of the package and contain an @Entity annotation

Parameters:
em - an open EntityManager
packageName - a package name to filter all entity classes known to the EntityManager
Returns:
a list of classes belonging to packageName

findAll

public static java.lang.String findAll(java.lang.Class entityClass)
Parameters:
entityClass - an entityClass
Returns:
select o from entityClass.getSimpleName() o

nativeQueryTester

public static void nativeQueryTester(javax.persistence.EntityManager em,
                                     java.lang.String sql,
                                     int max,
                                     java.lang.Class returnClass)
Parameters:
em - an open EntityManager
sql - a native SQL query
max - the maximum number of rows to read
returnClass - the class of the bean returned by the nativeQuery

queryTester

public static void queryTester(javax.persistence.EntityManager em,
                               java.lang.String jpql,
                               int max)
The code for this method was taken from Pro EJB 3: Java Persistence API by Keith and Schincariol published by apress 2006, pages 195-196.

Parameters:
em - an open EntityManager
jpql - a JPQL statement
max - the maximum number of rows to return

printAnyBean

public static void printAnyBean(java.lang.Object result)
                         throws java.lang.Exception
Used by queryTester()

Parameters:
result -
Throws:
java.lang.Exception
See Also:
queryTester(EntityManager, String, int)

getFirstSQLException

public static java.sql.SQLException getFirstSQLException(java.lang.Exception exception)
Parameters:
exception - any exception
Returns:
the first instance of an SQLException taken from exception or exception.getCause()

containsErrorCode

public static boolean containsErrorCode(java.lang.Exception exception,
                                        int code)
The method recursively checks exception and its causes for SQLExceptions with a matching code

Parameters:
exception -
code - a value to match to SQLException.getErrorCode()
Returns:
true
See Also:
SQLException.getErrorCode()

containsErrorCode

public static boolean containsErrorCode(java.lang.Exception exception,
                                        int[] codes)
The method recursively checks exception and its causes for SQLExceptions with a matching an element of codes

Parameters:
exception -
codes - an array of values to match to SQLException.getErrorCode()
Returns:
true
See Also:
SQLException.getErrorCode()

isSQLExceptionErrorCode

public static boolean isSQLExceptionErrorCode(java.lang.Exception exception,
                                              int errorCode)
The method recursively tests the exception and all causes looking for an SQLException with the given errorCode

Parameters:
exception -
errorCode - an SQL error code to test against
Returns:
true if the exception or its causes are SQLExceptions and have a matching errorCode
See Also:
#isSQLExceptionErrorCode(SQLException sqlException, int errorCode), isSQLExceptionErrorCode(Exception Exception, int[] errorCode)

isSQLExceptionErrorCode

public static boolean isSQLExceptionErrorCode(java.lang.Exception exception,
                                              int[] errorCodes)
Parameters:
exception -
errorCodes - an Array of error codes to check for
Returns: