JPA 已废弃的SQLQuery.class、setResultTransformer方法替换

@PersistenceContext
private EntityManager entityManager;

Query nativeQuery = entityManager.createNativeQuery(querySql.toString());

//查询转为map(key为字段名全大写)
org.hibernate.Query query = nativeQuery.unwrap(SQLQuery.class)
        .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

List> resultMap = query.getResultList();
-----------------------------------------------------------------------------
hibernate 5.2 之后,SQLQuery.class、setResultTransformer方法已作废
替换方法:
Query nativeQuery = entityManager.createNativeQuery(querySql.toString());

//查询转为map(key为字段名全大写)
NativeQueryImplementor nativeQueryImplementor = nativeQuery.unwrap(NativeQueryImpl.class)
        .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

List> resultMap = nativeQueryImplementor.getResultList();

但是,用NativeQueryImpl,在dubbo调用时会转换异常(com.sun.proxy.$Proxy313 cannot be cast to org.hibernate.query.internal.NativeQueryImpl),

只能用过时的SQLQuery!!!!!

 

你可能感兴趣的:(JPA 已废弃的SQLQuery.class、setResultTransformer方法替换)