How generate the SQL queries that match the JPA Entity CRUD operations at build time with Hibernate
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 开发者_Python百科appreciated.
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
精彩评论