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.

4

1 回答 1

0

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

于 2010-04-08T12:43:57.370 回答