简体   繁体   English

如何按 @ManyToMany 关系的 ID 对 Set 进行排序?

[英]How to sort a Set by its Id of a @ManyToMany relation?

I would like to sort a Set by its Id of a @ManyToMany relation in a web application that I am developing and I don't know how to do it.我想在我正在开发的 web 应用程序中按 @ManyToMany 关系的 ID 对 Set 进行排序,但我不知道该怎么做。 At the moment the Set is displayed in the order in which the ManyToMany relationships were created.目前 Set 以创建多对多关系的顺序显示。

Class Especie Class Especie

@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class Especie extends Familia{

    @GeneratedValue
    private Integer diasGerminacionDesde;
    private Integer diasGerminacionHasta;

    @ManyToOne
    private Familia familia;

    @JoinTable(
        name = "especie_mesesSiembra"
        ,joinColumns=@JoinColumn(name="especie_id")
        ,inverseJoinColumns=@JoinColumn(name="mesSiembra_id")
    )

    @ManyToMany
    private Set<MesSiembra> mesesSiembra;
}

Class EspecieControlador (Controller) Class EspecieControlador(控制器)

@GetMapping
public List<Especie> listarTodos() {
    return servicio.listarTodos();
}

Class EspecieServicio (Service) Class EspecieServicio(服务)

public List<Especie> listarTodos() {
    Sort orden = Sort.by(Sort.Direction.ASC, "nombre");
    return repositorio.findAll(orden);
}

You can use a LinkedHashSet + JPA annotation @OrderBy您可以使用 LinkedHashSet + JPA 注释 @OrderBy

@OrderBy
private Set<MesSiembra> mesesSiembra = new LinkedHashSet<>();

I have replaced this:我已经替换了这个:

@ManyToMany
private Set<MesSiembra> mesesSiembra;

For this:为了这:

@OrderBy("mesSiembra_id")
private Set<MesSiembra> mesesSiembra = new LinkedHashSet<>();

In the Spring Boot console I get the following error:在 Spring 引导控制台中,我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'especieRepositorio' defined in ar.com.mibancosemillas.bancosemillasweb.persistencia.EspecieRepositorio defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; org.springframework.beans.factory.BeanCreationException:在创建 ar.com.mibancosemillas.bancosemillasweb.persistencia.EspecieRepositories 中定义的 @EnableJpaRepositories 中定义的名称为“especieRepositorio”的 bean 时出错设置 bean 属性“mappingContext”; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: familia, for columns: [org.hibernate.mapping.Column(meses_siembra)]嵌套异常是 org.hibernate.MappingException:无法确定类型:java.util.Set,在表:familia,对于列:[org.ZCB1F008EEBF5012C4EF9A2C36E5744D6]。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM