Hibernate Criteraia无法从数据库中检索数据

柴坦亚

嗨,我是Hibernate的新手,因为我正在使用Criteria从数据库中检索数据,但是如果记录也匹配,则无法获取数据。以下是我使用的代码段。以下方法主要用于通过与(adminUser列的QuickUrlAdmin表)进行比较来检查列表(组)中的用户是否为admin。

但是我通过检查日志(数据库中存在记录)将request1的大小设置为0.任何建议

private boolean isAdmin() throws AppException {
try {
 Session session = PersistenceManager.getSession(); 
 Criteria criteria = session.createCriteria(QuickUrlAdmin.class);
             List<QuickUrl> request1 = null;             
             for (String group : groups) { //groups is a list  filled from another method 
                 log.debug("group:" + group);               
                 request1 =criteria.add(Restrictions.eq("adminUser", group)).list();
                 log.debug(" Request Object :" +request1.size());                
             }
}

直接HQL查询工作正常

String sql = "SELECT * FROM QuickUrlAdmin WHERE adminUser = :group_name";
             SQLQuery query = session.createSQLQuery(sql);
             query.addEntity(QuickUrlAdmin.class);           
                for (String group : groupList) {
                     log.debug("*********group:" + group);              
                     query.setParameter("group_name", group);
                     List<QuickUrl> results1 = query.list();                
                     log.debug(" Request Object :" +results1.size());
                 }         

控制台日志

09:34:34,479 INFO  [STDOUT] 09:34:34,479 DEBUG AppServiceImpl.isAdmin:335 - *********group:BES12_002 - Default
09:34:34,499 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=?
09:34:34,775 INFO  [STDOUT] 09:34:34,774 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:34,776 INFO  [STDOUT] 09:34:34,776 DEBUG AppServiceImpl.isAdmin:335 - *********group:BES200CNC Users
09:34:34,777 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:35,053 INFO  [STDOUT] 09:34:35,052 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:35,054 INFO  [STDOUT] 09:34:35,053 DEBUG AppServiceImpl.isAdmin:335 - *********group:ValueLabs Consultants - eCommerce BE
09:34:35,055 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:35,328 INFO  [STDOUT] 09:34:35,328 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:35,329 INFO  [STDOUT] 09:34:35,329 DEBUG AppServiceImpl.isAdmin:335 - *********group:Livelink - Consultant 2
09:34:35,331 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:35,605 INFO  [STDOUT] 09:34:35,603 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:35,606 INFO  [STDOUT] 09:34:35,606 DEBUG AppServiceImpl.isAdmin:335 - *********group:BTS-Hyderabad Team
09:34:35,608 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:35,882 INFO  [STDOUT] 09:34:35,881 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:35,883 INFO  [STDOUT] 09:34:35,883 DEBUG AppServiceImpl.isAdmin:335 - *********group:Team.josims
09:34:35,885 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:36,157 INFO  [STDOUT] 09:34:36,156 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:36,157 INFO  [STDOUT] 09:34:36,157 DEBUG AppServiceImpl.isAdmin:335 - *********group:BTS_VL_DEV
09:34:36,160 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:36,433 INFO  [STDOUT] 09:34:36,433 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:36,434 INFO  [STDOUT] 09:34:36,433 DEBUG AppServiceImpl.isAdmin:335 - *********group:Password Policy for Standard Users
09:34:36,435 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:36,713 INFO  [STDOUT] 09:34:36,713 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:36,714 INFO  [STDOUT] 09:34:36,714 DEBUG AppServiceImpl.isAdmin:335 - *********group:Team.jschen
09:34:36,716 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:36,989 INFO  [STDOUT] 09:34:36,989 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:36,990 INFO  [STDOUT] 09:34:36,990 DEBUG AppServiceImpl.isAdmin:335 - *********group:Team.mthota
09:34:36,992 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=?
09:34:37,264 INFO  [STDOUT] 09:34:37,264 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:37,265 INFO  [STDOUT] 09:34:37,265 DEBUG AppServiceImpl.isAdmin:335 - *********group:DirectReports.mthota
09:34:37,267 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:37,542 INFO  [STDOUT] 09:34:37,542 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:37,543 INFO  [STDOUT] 09:34:37,543 DEBUG AppServiceImpl.isAdmin:335 - *********group:Team.bmalencia
09:34:37,545 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:37,818 INFO  [STDOUT] 09:34:37,817 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:37,819 INFO  [STDOUT] 09:34:37,819 DEBUG AppServiceImpl.isAdmin:335 - *********group:Team.ropayne
09:34:37,821 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:38,095 INFO  [STDOUT] 09:34:38,094 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:38,096 INFO  [STDOUT] 09:34:38,096 DEBUG AppServiceImpl.isAdmin:335 - *********group:Team.sstrout
09:34:38,098 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:38,369 INFO  [STDOUT] 09:34:38,369 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:38,370 INFO  [STDOUT] 09:34:38,370 DEBUG AppServiceImpl.isAdmin:335 - *********group:EVPol5
09:34:38,371 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:38,646 INFO  [STDOUT] 09:34:38,646 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:38,647 INFO  [STDOUT] 09:34:38,647 DEBUG AppServiceImpl.isAdmin:335 - *********group:RIM INDIA - Consultants and Contractors
09:34:38,649 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:38,925 INFO  [STDOUT] 09:34:38,925 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:38,926 INFO  [STDOUT] 09:34:38,926 DEBUG AppServiceImpl.isAdmin:335 - *********group:BPM - RIM ALL
09:34:38,928 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:39,201 INFO  [STDOUT] 09:34:39,200 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:39,202 INFO  [STDOUT] 09:34:39,202 DEBUG AppServiceImpl.isAdmin:335 - *********group:JIRA Users
09:34:39,204 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:39,480 INFO  [STDOUT] 09:34:39,479 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:39,481 INFO  [STDOUT] 09:34:39,480 DEBUG AppServiceImpl.isAdmin:335 - *********group:Self Service Admin Portal
09:34:39,482 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:39,755 INFO  [STDOUT] 09:34:39,755 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:39,756 INFO  [STDOUT] 09:34:39,756 DEBUG AppServiceImpl.isAdmin:335 - *********group:CodeCollaborator Users
09:34:39,758 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:40,035 INFO  [STDOUT] 09:34:40,034 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:40,036 INFO  [STDOUT] 09:34:40,036 DEBUG AppServiceImpl.isAdmin:335 - *********group:External - Contractors and Consultants
09:34:40,038 INFO  [STDOUT] Hibernate: select this_.ID as ID12_0_, this_.ADMINUSER as ADMINUSER12_0_ from QUICKURL.QUICKURLADMIN this_ where this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADMINUSER=? and this_.ADM
09:34:40,313 INFO  [STDOUT] 09:34:40,312 DEBUG AppServiceImpl.isAdmin:337 -  Request Object :0
09:34:40,609 INFO  [STDOUT] 09:34:40,609 DEBUG AppServiceImpl.getLoginURL:62 - called
09:34:40,610 INFO  [STDOUT] 09:34:40,610 DEBUG AppServiceImpl.getAdminByKey:104 - called
09:34:40,612 INFO  [STDOUT] Hibernate: select this_.ID as ID10_0_, this_.KEY as KEY10_0_, this_.DATA_FIELD as DATA3_10_0_, this_.CREATED_DATE as CREATED4_10_0_, this_.MODIFIED_DATE as MODIFIED5_10_0_ from QUICKURL.ADMIN this_ where this_.KEY=?
09:34:41,454 INFO  [STDOUT] 09:34:41,453 DEBUG AppServiceImpl.getRegExNameValidation:76 - called
09:34:41,455 INFO  [STDOUT] 09:34:41,454 DEBUG AppServiceImpl.getAdminByKey:104 - called
09:34:41,457 INFO  [STDOUT] Hibernate: select this_.ID as ID10_0_, this_.KEY as KEY10_0_, this_.DATA_FIELD as DATA3_10_0_, this_.CREATED_DATE as CREATED4_10_0_, this_.MODIFIED_DATE as MODIFIED5_10_0_ from QUICKURL.ADMIN this_ where this_.KEY=?
09:34:42,267 INFO  [STDOUT] 09:34:42,266 DEBUG AppServiceImpl.getRegExUrlValidation:90 - called
09:34:42,268 INFO  [STDOUT] 09:34:42,267 DEBUG AppServiceImpl.getAdminByKey:104 - called
09:34:42,270 INFO  [STDOUT] Hibernate: select this_.ID as ID10_0_, this_.KEY as KEY10_0_, this_.DATA_FIELD as DATA3_10_0_, this_.CREATED_DATE as CREATED4_10_0_, this_.MODIFIED_DATE as MODIFIED5_10_0_ from QUICKURL.ADMIN this_ where this_.KEY=?
09:34:43,086 INFO  [STDOUT] 09:34:43,086 DEBUG ParseFieldAnnotation.getMetaInfo:23 - called
09:34:43,089 INFO  [STDOUT] 09:34:43,088 DEBUG ParseFieldAnnotation.getMetaInfo:43 - called
09:34:43,145 INFO  [STDOUT] Hibernate: select * from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_ID not in (select COLUMN_ID from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_NAME like ? and upper(DATA_TYPE)=?)
09:34:45,280 INFO  [STDOUT] Hibernate: select * from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_ID not in (select COLUMN_ID from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_NAME like ? and upper(DATA_TYPE)=?)
09:34:48,801 INFO  [STDOUT] Hibernate: select * from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_ID not in (select COLUMN_ID from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_NAME like ? and upper(DATA_TYPE)=?)
09:34:51,351 INFO  [STDOUT] Hibernate: select * from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_ID not in (select COLUMN_ID from USER_TAB_COLUMNS where TABLE_NAME=? and COLUMN_NAME like ? and upper(DATA_TYPE)=?)
09:34:53,702 INFO  [STDOUT] 09:34:53,701 DEBUG AppServiceImpl.getPagingQuickUrls:432 - config : limit=12  offset=0  sortField=null  sortDir=NONE  quickUrlPrefix=null  rimnetUserId=null  viewUnpublishedUrls=false  redirectQuery=null
安库·辛哈尔(Ankur Singhal)

我从你的问题中了解到的是

1.)您有一些名称列表groups

2)您正在获取QuickUrlAdmin,其中adminUser等于传递的名称。

基本上,您是逐组进行迭代,并且每次都访问数据库。

试试这个,会解决你的目的

SELECT * FROM QuickUrlAdmin WHERE adminUser IN :groups

团体只是那里list of String

修改后的代码

String sql = "SELECT * FROM QuickUrlAdmin WHERE adminUser IN :groups";
             SQLQuery query = session.createSQLQuery(sql);
             query.addEntity(QuickUrlAdmin.class);           
             query.setParameter("groups", groups);
             List<QuickUrl> results1 = query.list();  

标准API

private boolean isAdmin() throws AppException {
try {
 Session session = PersistenceManager.getSession(); 
 Criteria criteria = session.createCriteria(QuickUrlAdmin.class);
             List<QuickUrl> request1 = null;             
             request1 =criteria.add(Restrictions.in("adminUser", groups)).list();
             log.debug(" Request Object :" +request1.size());    
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Hibernate从数据库中检索元素

通过Hibernate从数据库检索数据的方法

无法使用 Hibernate 在数据库中创建表

Hibernate无法从数据库中获取SequenceInformation

从数据库中检索一行作为Hibernate中的Map

使用Hibernate从数据库中检索BLOB和使用JSP显示网页上

使用 Hibernate 从数据库中检索列表时出现 ClassCastException

无法使用spring-mvc和hibernate从数据库中获取数据

如何在Hibernate中从数据库中获取数据

从数据源Netbeans / Hibernate中“检索密钥”时,数据库中的实体类挂起

无法使用Hibernate连接到数据库

Hibernate @Column注解无法映射到数据库

以期从MySQL数据库检索使用springboot和Hibernate的日志数据

从 Hibernate 检索时会忽略具有换行符的数据库列数据吗?

Hibernate是否将从数据库检索的集合放到第一个lvl缓存中?

在数据库中搜索Like Hibernate的Integer

Hibernate应该只更新数据库中的某些字段

Hibernate Search不为数据库中的项目建立索引

Hibernate是否自动在数据库中创建表

Hibernate自己在数据库中创建列

Hibernate中的beginTransaction是否分配新的数据库连接?

在数据库中搜索文本(使用Hibernate Search)

无法从数据库中检索数据

Hibernate 重复的数据库条目

Hibernate的创建数据库冗余列

Sybase / hibernate数据库性能

Hibernate未更新数据库记录

Hibernate数据库关系注释

强制Hibernate查询访问数据库