2013-02-27 68 views
6

這裏是我的代碼下面給出dowork()即時通訊不能得到返回計數有沒有什麼辦法從這個方法得到返回和doreturnwork()不工作,請幫助我出去我嘗試,但沒有像doreturnwork沒有方法請幫助如何從doWork()方法返回

sessionFactory.getCurrentSession().doWork( 

      new Work() 
      { 
       public void execute(Connection connection) throws SQLException 
       { 
        String contactQueryCounts = ""; 
        String contactQueryIds = ""; 
        int index = 1; 
        StringBuilder builder = new StringBuilder(); 
        CustomerUser user = (CustomerUser) userService.getLoggedInUser(); 
        String inClause = ""; 
        for (Long id :ids) { 
         builder.append("?,"); 
        } 
        if(builder.length()>0){ 
         inClause = builder.substring(0, builder.length()-1); 
        } 

        if(inClause.length()>0){ 
         contactQueryCounts= "select count(id) from (select *,@num := if(@company_id = company_id, @num + 1, 1) as row_number," + 
         "@company_id := company_id as dummy from salebuild_ctl.company_contact where id in ("+inClause+") " + 
         " order by company_id, date_created asc ) as x where x.row_number <= ?"; 

         contactQueryIds= "select id from (select *,@num := if(@company_id = company_id, @num + 1, 1) as row_number," + 
         "@company_id := company_id as dummy from salebuild_ctl.company_contact where id in ("+inClause+") " + 
         " order by company_id, date_created asc ) as x where x.row_number <= ?"; 
        }else{ 
         contactQueryCounts= "select count(id) from (select *,@num := if(@company_id = company_id, @num + 1, 1) as row_number," + 
         "@company_id := company_id as dummy from salebuild_ctl.company_contact " + 
         " order by company_id, date_created asc ) as x where x.row_number <= ?"; 

         contactQueryIds= "select id from (select *,@num := if(@company_id = company_id, @num + 1, 1) as row_number," + 
         "@company_id := company_id as dummy from salebuild_ctl.company_contact " + 
         " order by company_id, date_created asc ) as x where x.row_number <= ?"; 
        } 

        java.sql.PreparedStatement sCount; 
        java.sql.PreparedStatement sIds; 
        try { 
         sCount = connection.prepareStatement(contactQueryCounts); 
         sIds = connection.prepareStatement(contactQueryIds); 
         for(Long id : ids){ 
          sCount.setLong(index, id); 
          sIds.setLong(index, id); 
          index=index+1; 
         } 
         sCount.setInt(index,user.getAccount().getCurrentSubscription().getNumberofcontactspercompany()); 
         sIds.setInt(index,user.getAccount().getCurrentSubscription().getNumberofcontactspercompany()); 
         ResultSet rs = sCount.executeQuery(); 
         int c = rs.getInt(1); 

         String contactLevelCountsQuery="select c.name,count(a.id) from company_contact a left join " + 
         "title b on a.corporate_title_id=b.id left join title_level c on b.title_level_id=c.id where a.id in" + 
         " ("+sIds.toString()+") and c.name is not null group by c.name"; 

         java.sql.Statement s = connection.createStatement(); 
         ResultSet rsIds = s.executeQuery(contactLevelCountsQuery); 

         SearchService.levelToContactCount.clear(); 

         while (rsIds.next()) { 
          if(rsIds.getString(1) == null){ 
           SearchService.levelToContactCount.put("No Level",rsIds.getInt(2)); 
          }else{ 
           SearchService.levelToContactCount.put(rsIds.getString(1),rsIds.getInt(2)); 
          } 
         } 
        } catch (SQLException e) { 
         e.printStackTrace(); 
        } 
        catch (Exception e) { 
         e.printStackTrace(); 
        } 
       }  
      } 
    ); 

回答

4

初始化一個空的集合或持有者對象,並填寫此集合或持有人的Work實例內:

public List<Foo> searchFoos() { 
    final List<Foo> result = new ArrayList<Foo>(); 
    sessionFactory.getCurrentSession().doWork(new Work() { 
     public void execute(Connection connection) throws SQLException { 
      // do some work 
      result.add(foo1); 
      // do some work 
      result.add(foo2); 
     } 
    }); 
    return result; 
} 

或者更清潔,創建一個Work的命名子類:

private static class FooWork implements Work { 
    private Foo result; 

    public void execute(Connection connection) throws SQLException { 
     // do some work 
     result = new Foo(...); 
    } 

    public Foo getResult() { 
     return result; 
    } 
} 

public Foo searchFoo() { 
    FooWork fooWork = new FooWork();   
    sessionFactory.getCurrentSession().doWork(fooWork); 
    return fooWork.getResult(); 
} 
+0

很好的解釋 – ronan 2017-04-07 18:46:47

0

也許您想在Hibernate中使用另一種名爲doReturningWork的方法。也許你可以看看this blog

+0

我已經在doReturingWork沒有在我的版本的工作描述中提到。 – shitanshu 2013-03-01 06:02:20

11

從Hibernate4,您還可以使用session.doReturningWork(ReturningWork工作)如下,

​​