【问题标题】:How to return the size of the greatest Collection in JPA?如何返回 JPA 中最大集合的大小?
【发布时间】:2014-10-24 05:30:25
【问题描述】:

这是我的代码:

@Entity
@Table("Books")
public class BookImp implements Book {

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

@ElementCollection
private Set<String> authors = new HashSet<String>();

// getters + constructors

}

我想创建一个 SQL 查询,它将返回最伟大作者集合的大小。我该怎么做?

编辑:对于那些建议使用 for 循环的人,这就是我希望答案的样子:

@Override
public int getLargestAuthors() {
return entityManager.createQuery("select b.authors from BookImpl b where b.authors.size ...").getSingleResult().size();
}

【问题讨论】:

    标签: java jpa jpql entitymanager


    【解决方案1】:

    不是 mysql 向导,但你不能这样做吗?

    select MAX(b.authors.size) from BookImpl b
    

    【讨论】:

    • 虽然你不是mysql精灵,但你的解决方案已经奏效了!谢谢!
    【解决方案2】:

    SELECT m FROM BookImp m where size(m.authors) = (select max(size(c.authors)) FROM BookImp c)

    这将返回一个或多个具有最大尺寸的 BookImp。

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多