【问题标题】:Hibernate HQL Count and Implicit Join result are differentHibernate HQL Count 和 Implicit Join 结果不同
【发布时间】:2017-09-28 00:13:30
【问题描述】:

我有 A 类 字段

Long id
B activity

及相关B类

Long id
String name

然后我有 HQL 查询:

select a.id, a.activity.name from A a;

类 A 上的一些 B 字段具有空值,因此 left join 提供的结果比内连接更多。

我正在自动生成 HQL,所以我想要选择然后计数:

select count(*) from A;

这给了我不同的结果。

除了在 SQL 上添加显式左连接(我正在接收 HQL,无法更改它)之外,有什么方法可以解决这个问题。

【问题讨论】:

    标签: java hibernate join


    【解决方案1】:

    尝试使用相同的查询执行计数,这样:

    Query query = getEm().createQuery("select a.id, a.activity.name from A a");
    
    int count = query.getResultList().size();
    

    或者,如果您想创建native query,请配置您的persistence.xml 以启用日志语句。所以你可以将原来的select修改成countStatement logging and statistics

    • hibernate.show_sql
    • hibernate.format_sql
    • hibernate.use_sql_comments

      <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
          <property name="show_sql" value="true" />
          <property name="format_sql" value="true" />
          <property name="hibernate.hbm2ddl.auto" value="update" />
      </properties>
      

    【讨论】:

      猜你喜欢
      • 2013-08-25
      • 2013-09-18
      • 1970-01-01
      • 2016-09-14
      • 2013-08-30
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多