Hibernate WHERE不适用于@Embedded和null成员

时间:2016-07-05 15:08:14

标签: java spring hibernate

我有一个Posting的课程@Embedded

public class Posting {
  @Embedded
  private BankDetails bankDetails;      
}

public class BankDetails {
    private String bankCode;
    private String bankName;
}

现在,我想列出Posting bankCode 123bankName null的所有Query q = entityManager.createQuery("SELECT o from Posting o WHERE bankDetails=?"); q.setParameter(1, new BankDetails("123", null));

select posting0_.bank_code as bank_cod6_42_, posting0_.bank_name as bank_nam7_42_
from posting posting0_ 
where (posting0_.bank_code, posting0_.bank_name)=('123', null)

但是我没有得到任何结果。

为什么?

这是Hibernate执行的SQL查询:

null

(顺便说一句,这个括号语法称为行值构造函数。)

这显然不起作用,因为将=["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"] 进行比较。

  1. 这是一个错误吗?如果是,哪个组件生成SQL语句? (Hibernate?mysql-connector?)
  2. 有关解决方法的任何想法吗?
  3. 环境:Win10上的MySQL Server 5.7,Spring 4.0.3,Hibernate 4.3.11,mysql-connector 5.1.30

2 个答案:

答案 0 :(得分:1)

是的,有一种解决方法,但它要求您对查询更加冗长。

entityManager.createQuery( "SELECT o FROM Posting o WHERE " +
    " o.bankDetails.bankCode = ? " +
    " AND o.bankDetails.bankName IS NULL" );
entityManager.setParameter( 1, "123" );

老实说,我没有看到使用这样的元组值生成的查询,但我过去的大部分经验都是SQL Server不支持这样的语法。如果您认为这是一个错误,请随时打开JIRA问题。

答案 1 :(得分:0)

这是Hibernate 4和5中的错误

应该在Hibernate 6中修复:https://hibernate.atlassian.net/browse/HHH-8172

请参阅@Naros答案以获取解决方法。