6

这是我在下面给出的 dowork() 代码我无法获得返回计数有没有办法从这个方法中获得返回并且 doreturnwork() 对此不起作用请帮助我尝试它但是没有像 doreturnwork 这样的方法请帮忙

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

3 回答 3

12

从 Hibernate4,您还可以使用 session.doReturningWork(ReturningWork work) 如下,

ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {

                @Override
                public Long execute(Connection connection) throws SQLException {
                    PreparedStatement preparedStatement = null;
                    ResultSet resultSet = null;
                    try {
                        preparedStatement = connection.prepareStatement("select max(salary) from employee");
                        resultSet = preparedStatement.executeQuery();
                        resultSet.next();
                        return resultSet.getLong(1);
                    }catch (SQLException e) {
                        throw e;
                    } finally {
                        if(preparedStatement != null) {
                            preparedStatement.close();
                        }
                        if(resultSet != null) {
                            resultSet.close();
                        }
                    }

                }
            };
            Long maxRecord = getSession().doReturningWork(maxReturningWork);
于 2014-01-25T10:29:21.180 回答
5

初始化一个空的集合或持有者对象,并在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();
}
于 2013-02-27T12:56:09.897 回答
0

可能您想在 Hibernate中使用另一种称为doReturningWork的方法。也许你可以看看这个博客

于 2013-02-28T08:22:03.470 回答