开发者

How generate the SQL queries that match the JPA Entity CRUD operations at build time with Hibernate

开发者 https://www.devze.com 2022-12-24 19:23 出处:网络
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) ?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消