In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how JPA query native SQL conversion VO object way, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
JPA query native SQL transform VO object List sqlList = RiverCourseSql.getRiverCourseSQLString (new RiverCourseDataQO ()); List riverCourseDataVO = rsvrfsrBDao.executeNativeQuery2Obj (sqlList.get (0), new PageQO (), RiverCourseDataVO.class)
Query the sql, and then execute the functional method.
List executeNativeQuery2Obj (String sql, SortQO sortQO, Class clz); public List executeNativeQuery2Obj (String sql, SortQO sortQO, Class clz) {Query query = this.entityManager.createNativeQuery (sql); this.setAliasParameter (query, sortQO); ((NativeQueryImpl) query.unwrap (NativeQueryImpl.class)) .setResultTransformer (Transformers.aliasToBean (clz)); List resultList = query.getResultList (); some points for attention of return resultList;} JPA unannotated native sql query
Background: there are complex aggregate statistics in DDD system, so it is not convenient for code to assemble data.
Import javax.persistence.EntityManager;import javax.persistence.Query;@Autowiredprivate EntityManager entityManager
This is directly received with a custom VO. The result set returned by sql must be a subset of VO, otherwise the assembly data will report an error. To put it bluntly, all the fields of sql query results must be corresponding in vo, and vo can have extra fields, but sql results cannot have extra fields.
String sql = "sql"; Query nativeQuery = entityManager.createNativeQuery (sql)
Set the parameter for the dynamic parameter passing in sql: = param. Note that if the array is passed after in, some should be parenthesized and some should not be added, depending on the version. Just pass the data in.
NativeQuery.setParameter (String var1, Object var2)
This is the current non-obsolete method to convert sql query results into custom VO
Transformers can be converted to list and vo,nativeQueryImplementor as well as list and vo
NativeQueryImplementor nativeQueryImplementor = nativeQuery.unwrap (NativeQueryImpl.class) .setResultTransformer (Transformers.aliasToBean (VO.class)); VO vo = new VO (); try {/ / there is a hole here. If the returned result is empty, an error will be reported. To catch this exception, further operation vo = (VO) nativeQueryImplementor.getSingleResult ();} catch (EntityNotFoundException e) {e.printStackTrace (); return vo;}
That's all for now.
These are all the contents of the article "how JPA queries the way native SQL converts VO objects". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.