hibernate_query.list()返回的数据类型

本文介绍了在Hibernate中使用HQL进行实体类查询的方法,并详细解释了如何通过不同的HQL语句获取不同形式的数据集合,包括List、Object及Object[]等。针对特定场景,提出了使用select new语句结合实体类带参构造函数来简化查询结果处理的有效方案。

 在hibernate中,用hql语句查询实体类,采用list方法的返回结果一个为List,该List中封装的对象分为以下三种情况。

1. 查询全部字段的情况下,如”from 实体类”,list中封装的对象为实体类本身,各属性都将得到填充。
2. 只查询一个字段,默认情况下,list中封装的是Object对象。
3. 查询两个或两个以上的字段,默认情况下封装的是Object[],长度与查询的字段书一致。

 对于后两种情况,用标签遍历时不太方便,因为无法直接转换成实体类的对象。
 比较简单的解决方案是:在hql中使用select new 包名.类名(属性1,属性2···) from 实体类,同时,在实体类中添加带参的构造方法,参数的个数和顺序与(属性1,属性2···)保持一致,这样我们得到的list中存放的依然是实体类的对象,所查询的属性得到了填充,使用起来更加方便。

public int Handle_TrackIn_No2d(EQP_SECS_CONFIG esc, string ppid, StripMapInfoEntity mapInfo, string event_name) { int result = -1; try { using (var _ordb = new SessionManager().OpenReadSession()) using (var tran = _ordb.BeginTransaction()) { try { string time_key = TimeUtil.GetNowDateTime_20(); DateTime now_date = DateTime.Now; //select MES_LOT MES_LOT_HIST mes_lot_hist = new MES_LOT_HIST(); mes_lot_hist.TIME_KEY = time_key; mes_lot_hist.LOT_ID = mapInfo.LOT_ID; mes_lot_hist.LOT_QTY = mapInfo.LOT_QTY; mes_lot_hist.LOT_STATE = mapInfo.LOT_STATE; mes_lot_hist.CURR_PASS = mapInfo.CURR_PASS; mes_lot_hist.TOTAL_PASS = mapInfo.TOTAL_PASS; mes_lot_hist.REWORK_FLAG = mapInfo.REWORK_FLAG; mes_lot_hist.MULTI_MIX_FLAG = mapInfo.MULTI_MIX_FLAG; mes_lot_hist.EQP_ID = mapInfo.EQP_ID; mes_lot_hist.MEMBRANCE_TYPE = mapInfo.MEMBRANCE_TYPE; mes_lot_hist.MEMBRANCE_VALIDITY = mapInfo.MEMBRANCE_VALIDITY; mes_lot_hist.MILL_WHEEL1 = mapInfo.MILL_WHEEL1; mes_lot_hist.MILL_WHEEL2 = mapInfo.MILL_WHEEL2; mes_lot_hist.BLADE1 = mapInfo.BLADE1; mes_lot_hist.BLADE2 = mapInfo.BLADE2; mes_lot_hist.SUCTION_NOZZLE = mapInfo.SUCTION_NOZZLE; mes_lot_hist.THIMBLE = mapInfo.THIMBLE; mes_lot_hist.DISPENDSING_HEAD = mapInfo.DISPENDSING_HEAD; mes_lot_hist.CLEAVER = mapInfo.CLEAVER; mes_lot_hist.MOULD = mapInfo.MOULD; mes_lot_hist.SUBSTRATE_THICKNESS = mapInfo.SUBSTRATE_THICKNESS; mes_lot_hist.MCP = mapInfo.MCP; mes_lot_hist.PKG_GROUP = mapInfo.PKG_GROUP; mes_lot_hist.PKG_TYPE = mapInfo.PKG_TYPE; mes_lot_hist.CUST_CODE = mapInfo.CUST_CODE; mes_lot_hist.CUST_DEVICE = mapInfo.CUST_DEVICE; mes_lot_hist.DEVICE_NAME = mapInfo.DEVICE_NAME; mes_lot_hist.DIE_PART = mapInfo.DIE_PART; mes_lot_hist.DEVICE_TYPE = mapInfo.DEVICE_TYPE; mes_lot_hist.WAFER_SIZE = mapInfo.WAFER_SIZE; mes_lot_hist.EVENT_USER = mapInfo.EQP_ID; mes_lot_hist.EVENT_NAME = event_name; mes_lot_hist.EVENT_TIME = now_date; _ordb.SaveAsync(mes_lot_hist); MES_LOT mes_lot = new MES_LOT(); mes_lot = _ordb.CreateQuery("from MES_LOT where LOT_ID = :LOT_ID") .SetString("LOT_ID", mapInfo.LOT_ID) .List<MES_LOT>().FirstOrDefault(); if (mes_lot == null) { mes_lot = new MES_LOT(); mes_lot.LOT_ID = mapInfo.LOT_ID; mes_lot.LOT_TYPE = mapInfo.LOT_TYPE; mes_lot.PROD_TYPE = mapInfo.PROD_TYPE; mes_lot.CUST_ID = mapInfo.CUST_ID; mes_lot.PKG_ID = mapInfo.PKG_ID; mes_lot.DEVICE_ID = mapInfo.PROD_ID; mes_lot.PKG_SIZE = mapInfo.PKG_SIZE; mes_lot.DIE_SIZE = mapInfo.DIE_SIZE; mes_lot.THICKNESS = mapInfo.THICKNESS; mes_lot.STRIP_PN = mapInfo.STRIP_PN; mes_lot.EPOXY_PN = mapInfo.EPOXY_PN; mes_lot.WIRE_PN = mapInfo.WIRE_PN; mes_lot.EMC_PN = mapInfo.EMC_PN; mes_lot.LOT_QTY = mapInfo.LOT_QTY; mes_lot.LOT_ORDER_LINE = mapInfo.LOT_ORDER_LINE; mes_lot.MEMBRANCE_TYPE = mapInfo.MEMBRANCE_TYPE; mes_lot.MEMBRANCE_VALIDITY = mapInfo.MEMBRANCE_VALIDITY; mes_lot.MILL_WHEEL1 = mapInfo.MILL_WHEEL1; mes_lot.MILL_WHEEL2 = mapInfo.MILL_WHEEL2; mes_lot.BLADE1 = mapInfo.BLADE1; mes_lot.BLADE2 = mapInfo.BLADE2; mes_lot.SUCTION_NOZZLE = mapInfo.SUCTION_NOZZLE; mes_lot.THIMBLE = mapInfo.THIMBLE; mes_lot.DISPENDSING_HEAD = mapInfo.DISPENDSING_HEAD; mes_lot.CLEAVER = mapInfo.CLEAVER; mes_lot.MOULD = mapInfo.MOULD; mes_lot.SUBSTRATE_THICKNESS = mapInfo.SUBSTRATE_THICKNESS; mes_lot.MCP = mapInfo.MCP; mes_lot.REWORK_FLAG = mapInfo.REWORK_FLAG; mes_lot.PKG_GROUP = mapInfo.PKG_GROUP; mes_lot.PKG_TYPE = mapInfo.PKG_TYPE; mes_lot.CUST_CODE = mapInfo.CUST_CODE; mes_lot.CUST_DEVICE = mapInfo.CUST_DEVICE; mes_lot.DEVICE_NAME = mapInfo.DEVICE_NAME; mes_lot.DIE_PART = mapInfo.DIE_PART; mes_lot.DEVICE_TYPE = mapInfo.DEVICE_TYPE; mes_lot.WAFER_SIZE = mapInfo.WAFER_SIZE; mes_lot.EVENT_USER = mapInfo.EQP_ID; mes_lot.EVENT_NAME = event_name; mes_lot.EVENT_TIME = now_date; _ordb.SaveAsync(mes_lot); } else { mes_lot.LOT_ID = mapInfo.LOT_ID; mes_lot.LOT_TYPE = mapInfo.LOT_TYPE; mes_lot.PROD_TYPE = mapInfo.PROD_TYPE; mes_lot.CUST_ID = mapInfo.CUST_ID; mes_lot.PKG_ID = mapInfo.PKG_ID; mes_lot.DEVICE_ID = mapInfo.PROD_ID; mes_lot.PKG_SIZE = mapInfo.PKG_SIZE; mes_lot.DIE_SIZE = mapInfo.DIE_SIZE; mes_lot.THICKNESS = mapInfo.THICKNESS; mes_lot.STRIP_PN = mapInfo.STRIP_PN; mes_lot.EPOXY_PN = mapInfo.EPOXY_PN; mes_lot.WIRE_PN = mapInfo.WIRE_PN; mes_lot.EMC_PN = mapInfo.EMC_PN; mes_lot.LOT_QTY = mapInfo.LOT_QTY; mes_lot.LOT_ORDER_LINE = mapInfo.LOT_ORDER_LINE; mes_lot.MEMBRANCE_TYPE = mapInfo.MEMBRANCE_TYPE; mes_lot.MEMBRANCE_VALIDITY = mapInfo.MEMBRANCE_VALIDITY; mes_lot.MILL_WHEEL1 = mapInfo.MILL_WHEEL1; mes_lot.MILL_WHEEL2 = mapInfo.MILL_WHEEL2; mes_lot.BLADE1 = mapInfo.BLADE1; mes_lot.BLADE2 = mapInfo.BLADE2; mes_lot.SUCTION_NOZZLE = mapInfo.SUCTION_NOZZLE; mes_lot.THIMBLE = mapInfo.THIMBLE; mes_lot.DISPENDSING_HEAD = mapInfo.DISPENDSING_HEAD; mes_lot.CLEAVER = mapInfo.CLEAVER; mes_lot.MOULD = mapInfo.MOULD; mes_lot.SUBSTRATE_THICKNESS = mapInfo.SUBSTRATE_THICKNESS; mes_lot.MCP = mapInfo.MCP; mes_lot.REWORK_FLAG = mapInfo.REWORK_FLAG; mes_lot.PKG_GROUP = mapInfo.PKG_GROUP; mes_lot.PKG_TYPE = mapInfo.PKG_TYPE; mes_lot.CUST_CODE = mapInfo.CUST_CODE; mes_lot.CUST_DEVICE = mapInfo.CUST_DEVICE; mes_lot.DEVICE_NAME = mapInfo.DEVICE_NAME; mes_lot.DIE_PART = mapInfo.DIE_PART; mes_lot.DEVICE_TYPE = mapInfo.DEVICE_TYPE; mes_lot.WAFER_SIZE = mapInfo.WAFER_SIZE; mes_lot.EVENT_USER = mapInfo.EQP_ID; mes_lot.EVENT_NAME = event_name; mes_lot.EVENT_TIME = now_date; _ordb.UpdateAsync(mes_lot); } _ordb.FlushAsync(); tran.Commit(); result = 1; } catch (HibernateException e) { tran.Rollback(); throw e; } } } catch (Exception ex) { LoggerUtil.Trace(tag, System.Reflection.MethodBase.GetCurrentMethod().Name + $" Error {esc.EQP_ID}", ex); } return result; } 这种业务如何修改为读写分离
06-04
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:103) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:37) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:67) at org.hibernate.loader.Loader.getResultSet(Loader.java:2322) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2075) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2037) at org.hibernate.loader.Loader.doQuery(Loader.java:956) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357) at org.hibernate.loader.Loader.doList(Loader.java:2868) at org.hibernate.loader.Loader.doList(Loader.java:2850) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2682) at org.hibernate.loader.Loader.list(Loader.java:2677) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:540) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400) at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1459) at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1649) at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1617) at org.hibernate.query.Query.getResultList(Query.java:165) at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:128) at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90) at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:155) at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:160) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:81) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ... 186 more Caused by: com.kingbase8.util.KSQLException: ERROR: operator does not exist: text ~~ boolean Hint: No operator matches the given name and argument types. You might need to add explicit type casts. Position: 877 At Line: 1, Line Position: 877 报错了
最新发布
11-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值