【问题标题】:Hibernate criteraia not retrieving the data from database休眠标准不从数据库中检索数据
【发布时间】:2014-08-27 03:10:48
【问题描述】:

嗨,我是 Hibernate 的新手,因为我正在使用标准从数据库中检索数据,但如果记录也匹配,我无法获取数据。下面是我使用的代码sn-p。下面的方法主要是通过与(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

【问题讨论】:

  • 嗨,我在 QuickUrlAdmin 类上创建了一个标准,如下所示 Criteria criteria = session.createCriteria(QuickUrlAdmin.class);列表 request1 = null;
  • 我在问为什么我上面评论中使用的类有差异。
  • 我将 Criteria 添加到 QuickUrlAdmin 但我将结果列表类型转换为 QuickUrl 我们可以将两个类添加到一个标准吗?,我尝试使用上面的直接 HQL 查询,但我无法通过条件得到相同的结果
  • @chaitanaya 尝试使用 List,您是否在服务器控制台或日志中看到生成的查询,您是否使用 show_sql = true 启用了查询日志记录
  • 我在 hibernate 配置文件中修改为 show_sql=true 但是你能告诉我如何查看条件查询命中数据库

标签: hibernate


【解决方案1】:

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

1.) 你有一些名字列表,groups

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

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

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

SELECT * FROM QuickUrlAdmin WHERE adminUser IN :groups

其中 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());    
}

【讨论】:

  • 感谢 Ankur 修改后的 Criteria API 没有问题,我能够以有效的方式与数据库进行比较,再次感谢您对它的研究和帮助。
猜你喜欢
  • 2021-10-14
  • 2014-08-08
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多