views:

133

answers:

1

Given JPA annotated Entities, is it possible to generate (i.e. before runtime) the list of queries that will be performed by Hibernate for CRUD operations (performed against EntityManager) ? For named queries it is possible using org.hibernate.hql.QueryTranslator

Any pointer into the Hibernate API will be appreciated.

A: 

It is possible through hibernate ClassMetadata.

Session session = (Session) entityManager.getDelegate();
SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) session.getSessionFactory();
ClassMetadata hibernateMetadata = sessionFactory.getClassMetadata(pEntityClass.getName());
if (hibernateMetadata instanceof AbstractEntityPersister) {
   /*...look at protected methods that return SQL Strings for the entity getSQLIdentityInsertString,getSQLLazySelectString,getSQLSnapshotSelectString,getSQLUpdateByRowIdStrings,getSQLLazyUpdateByRowIdStrings,getSQLDeleteStrings,getSQLInsertStrings,getSQLUpdateStrings,getSQLLazyUpdateStrings */
}

Look at following link in order to access to protected methods: http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html

snowflake