[英]Hibernate sort entities by property of children mapped as Map<String, Child>
Here is my model:这是我的模型:
@Entity
@Table(name = "parent")
public class Parent {
...
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, cascade = { CascadeType.REMOVE}, mappedBy = "parent")
@MapKeyColumn(name = "key")
private Map<String, Child> children;
...
}
@Entity
@Table(name = "child")
public class Child {
...
@Column(name = "key")
private String key;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Parent parent;
@Column(name = "property")
private String property;
...
}
That mapping is usefull and works on inserting, updating, deleting entites and selecting them.该映射很有用,可用于插入、更新、删除实体和选择它们。
Now I have to order Parent entites using their children property depending on map key, a something like order by parent.children[<particular key>].property asc
clause(which I did not find in HQL).现在我必须根据地图键使用他们的子属性来订购父实体,类似于
order by parent.children[<particular key>].property asc
子句的order by parent.children[<particular key>].property asc
(我在 HQL 中没有找到)。 Also I have no idea how to achieve this using Criteria API.我也不知道如何使用 Criteria API 来实现这一点。 Any idea?
任何的想法?
Only solution I have now is to select Children filtered by key
and ordered by property
and then fetch parents in Java code one by one which is not efficient even with "fetch parent" set.我现在唯一的解决方案是选择
key
过滤并按property
排序的子项,然后在 Java 代码中一一获取父项,即使设置了“获取父项”也效率不高。
The table created statements come out like this:表创建的语句出来是这样的:
create table child (id bigint not null, key varchar(255), property varchar(255), parent_id bigint, primary key (id))
create table parent (id bigint not null, primary key (id))
with a constraint on parent_id to the parent table.对父表的 parent_id 约束。
Working backwards, the sql seems pretty straight forward:向后工作,sql 看起来很简单:
select p.* from parent p join child c on p.id = c.parent_id where c.key = 'c1' order by c.property asc;
And so the JPQL and Criteria query should also be straight forward:因此 JPQL 和 Criteria 查询也应该是直截了当的:
List<Parent> po1 = em.createQuery("select p from Parent p join p.children c where c.key='c1' order by c.property asc", Parent.class).getResultList();
System.out.println(po1);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Parent> cq = cb.createQuery(Parent.class);
Root<Parent> p = cq.from(Parent.class);
Join<Parent, Child> c = p.join("children");
cq.select(p).where(cb.equal(c.get("key"), "c1")).orderBy(cb.asc(c.get("property")));
List<Parent> po3 = em.createQuery(cq).getResultList();
System.out.println(po3);
Create a toString method for Parent:为 Parent 创建一个 toString 方法:
public String toString() {
return "Parent:"+id+":"+children;
}
and Child:和孩子:
public String toString() {
return "Child:"+id+":"+key+":"+property;
}
And that gives me the following output:这给了我以下输出:
Hibernate: select parent0_.id as id1_1_ from parent parent0_ inner join child children1_ on parent0_.id=children1_.parent_id where children1_.key='c1' order by children1_.property asc
[Parent:4:{c1=Child:5:c1:0}, Parent:1:{c1=Child:2:c1:1}]
Hibernate: select parent0_.id as id1_1_ from parent parent0_ inner join child children1_ on parent0_.id=children1_.parent_id where children1_.key=? order by children1_.property asc
[Parent:4:{c1=Child:5:c1:0}, Parent:1:{c1=Child:2:c1:1}]
Which looks to me to be the parents ordered by the child property where the key is a certain value.在我看来,这是由子属性排序的父项,其中键是某个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.