简体   繁体   English

Hibernate 按映射为 Map 的子项的属性对实体进行排序<String, Child>

[英]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.

相关问题 java / hibernate-如何使用注释映射三个表/实体(休眠错误未映射到单个属性) - java/hibernate - how to map three tables/entities with annotation (hibernate error not mapped to a single property) 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok 未在cfg中映射的休眠加载实体 - Hibernate loading Entities that are not mapped in cfg 如何获取所有子实体的特定属性作为休眠中的列表? - How to get a particular property of all child entities as list in hibernate? 如何在休眠状态下将实体属性映射到继承SINGLE_TABLE的实体? - How to map entity property to entities of inheritance SINGLE_TABLE in hibernate? Spring Hibernate:涉及映射实体的SQLQuery - Spring Hibernate: SQLQuery involving mapped entities Config Spring用于使用hibernate和通过注释映射的实体 - Config Spring for works with hibernate and entities mapped by annotations Hibernate 获取映射实体的所有外键 - Hibernate get all foreign keys for mapped entities 实体无法通过休眠注释正确映射 - Entities cannot be mapped properly via hibernate annotations 由其他许多不同实体映射的实体(使用Hibernate) - Entity mapped by other many different Entities (with Hibernate)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM