在休眠中选择NOT NOTIST

时间:2014-11-13 11:05:58

标签: hibernate

我想在持久层中通过hibernate执行这个sql查询,并使用此查询结果来更新book。我在Mysql工作台中使用了这个查询,它运行正常。查询如下:

"SELECT NOT EXISTS("+
                        "SELECT delivery_request_id FROM delivery_request WHERE book_id= "+book.getBookId()+" AND status LIKE 'PENDING')"+
                        "AND"+
                        "(SELECT NOT EXISTS(SELECT return_request_id FROM return_request rr WHERE rr.book_id="+book.getBookId()+" AND rr.delivery_request_id NOT IN ("+
                        "SELECT dr.delivery_request_id FROM delivery_request dr WHERE dr.book_id= "+book.getBookId()+" AND dr.status LIKE 'CLOSED')))"+   
                        "AND"+
                        "(SELECT NOT EXISTS"+
                        "(SELECT return_request_id FROM return_request WHERE book_id= "+book.getBookId()+" AND (status LIKE 'PENDING' OR status LIKE 'CANCELLED') ))"

我尝试session.createSQLQuery()但不知道用什么来执行此操作。那么如何执行此查询。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我设法运行它。 这是代码:

BigInteger result = (BigInteger) localSession.createSQLQuery("SELECT NOT EXISTS("+
                    "SELECT delivery_request_id FROM delivery_request WHERE book_id= "+book.getBookId()+" AND status LIKE 'PENDING')"+
                    "AND"+
                    "(SELECT NOT EXISTS(SELECT return_request_id FROM return_request rr WHERE rr.book_id="+book.getBookId()+" AND rr.delivery_request_id NOT IN ("+
                    "SELECT dr.delivery_request_id FROM delivery_request dr WHERE dr.book_id= "+book.getBookId()+" AND dr.status LIKE 'CLOSED')))"+   
                    "AND"+
                    "(SELECT NOT EXISTS"+
                    "(SELECT return_request_id FROM return_request WHERE book_id= "+book.getBookId()+" AND (status LIKE 'PENDING' OR status LIKE 'CANCELLED') ))").uniqueResult();
boolean exists = result.signum()>0;    

现在我可以使用存在值来进行更进一步的操作。