Hibernate标准不从数据库中检索数据

时间:2014-08-27 03:10:48

标签: hibernate

您好我是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

1 个答案:

答案 0 :(得分:0)

我从你的问题中理解的是

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