hibernate query

I was not very familiar with hibernate jpa at the beginning, and was very fond of sql, and then found that jpa queries could not be encapsulated into custom vos (this is a misunderstanding, it can be injected in the form of jpa queries and then constructors), after thinking about it, forget it , encapsulate a method yourself and directly convert it into a map collection and then encapsulate it into the collection.
//Implement map to javabean conversion for compatibility with hibernate custom sql package to bean
public List findPageBeanMapBySQL(String sql, Pageable pageable,Class clazz) {
Query query = entityManager.createNativeQuery(sql);
query.unwrap(SQLQuery.class) .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
if(pageable!=null){
long total = query.getResultList().size();
query.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize());
query.setMaxResults(pageable.getPageSize());
}

List<T> list = query.getResultList();

List lists = Lists.newArrayList();
if(list!=null && list.size()>0){
for(T t:list){
BeanWrapper beanWrapper = new BeanWrapperImpl(clazz);
beanWrapper.setPropertyValues((Map<?, ?>) t);
lists.add( beanWrapper.getWrappedInstance());
}
}

return lists;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326964785&siteId=291194637