简体   繁体   English

@OneToMany 中的 JPA OrderBy 外部字段

[英]JPA OrderBy in @OneToMany by external field

I have entities:我有实体:

@Entity
public class C {

 @Column
 private String name;

}


@Entity
public class B {

 @Column
 private Integer id;

 @ManyToOne
 @JoinColumn(name = "id_c")
 private C c;

}


@Entity
public class A {

 @OneToMany(mappedBy = "a")
 @OrderBy("id")
 private Set<B> itemsB;

}

Now when i access to A.itemsB() - items ordered by B.id现在,当我访问 A.itemsB() - 按 B.id 订购的项目时

I need to get A.itemsB() ordered by C.name.我需要按 C.name 订购 A.itemsB()。 Is this possible?这可能吗?

I tried to write something like @OrderBy("c.name") but it not work.我试图写一些类似 @OrderBy("c.name") 的东西,但它不起作用。

只需检查导入的 Order : org.hibernate.annotations.OrderBy 或 javax.persistence.OrderBy .. 你应该使用第二个。

您不应使用“Set<B>”,而应使用“List<B>”。集合始终是无序的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA-从查询中强制转换@OneToMany字段 - JPA - Casting @OneToMany field from Query 如何在 JPA Enity 中获取 OneToMany 字段的计数? - How to get Count of OneToMany field in JPA Enity? JPA OneToMany,在控制台或Webapp中打印时如何忽略字段ManyToOne - JPA OneToMany , how to ignore field ManyToOne when print in console or webapp Hibernate / JPA查询,其中包含列表字段中元素的条件(称为OneToMany) - Hibernate/JPA query with criteria for elements in list field (referenced as OneToMany) 如何使用 JPA 中的 SqlResultSetMapping 将本机 sql 结果映射到 oneToMany 字段 - How to map native sql results to oneToMany field with SqlResultSetMapping in JPA Spring JPA 条件 API 查询 OneToMany 列表字段 - Spring JPA Criteria API Query on OneToMany List Field 使用 JPA 规范(CriteriaQuery CriteriaBuilder)按过滤后的 OneToMany 关系中的字段排序 - order by a field in a filtered OneToMany relation using JPA Specification ( CriteriaQuery CriteriaBuilder ) ModelMapper &amp; JPA:如何将带有 id 的 map DTO 转换为带有 oneToMany 字段的实体 - ModelMapper & JPA: How to map DTO with id to Entity with oneToMany field 如何过滤 spring 数据 JPA 中的 @OneToMany 映射字段? - How to filter a @OneToMany mapped field in spring data JPA? 尝试按JPA Spring Boot中的嵌套字段排序时,OrderBy不起作用 - OrderBy not working while trying to sort by a nested field in JPA Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM