hibernate - 如何从doWork()方法获得返回

标签 hibernate

这是我下面为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();
                    }
                }    
            }
    );

最佳答案

在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);

关于hibernate - 如何从doWork()方法获得返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15112470/

相关文章:

java - 在 hibernate/jpa 实体类中定义 save 方法是一个好的约定吗?

spring - JPA Spring存储库过滤多个参数

java - Hibernate JPA ManyToOne 组合键

java - JPA 2.0 - 使用@NamedNativeQuery 的 Oracle 函数 - 错误

java - 如何告诉 hibernate validator 仅验证一个注释?

java - 如何减少 hibernate 集合的索引大小(使用复合元素设置)?

java - HTTP 状态 500 - 未能延迟初始化角色 : . 的集合 ..,无法初始化代理 - 无 session

java - 使用 SQL 将 Date Java 与数据库表中的 DateTime 列进行比较

hibernate - JPA 2.0/hibernate : No supertype found

java - 批量插入多对多关系