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

huangapple go评论75阅读模式
英文:

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

问题

我想按照 @ManyToMany 关系的 Id 对 Set 进行排序,但我不知道如何做。目前,Set 按照 ManyToMany 关系创建的顺序显示。

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;
}

EspecieControlador 类 (Controller)

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

EspecieServicio 类 (Service)

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

请注意,我在代码中只翻译了注释部分,其他代码部分保持不变。

英文:

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. At the moment the Set is displayed in the order in which the ManyToMany relationships were created.

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 = &quot;especie_mesesSiembra&quot;
        ,joinColumns=@JoinColumn(name=&quot;especie_id&quot;)
        ,inverseJoinColumns=@JoinColumn(name=&quot;mesSiembra_id&quot;)
    )

    @ManyToMany
    private Set&lt;MesSiembra&gt; mesesSiembra;
}

Class EspecieControlador (Controller)

@GetMapping
public List&lt;Especie&gt; listarTodos() {
	return servicio.listarTodos();
}

Class EspecieServicio (Service)

public List&lt;Especie&gt; listarTodos() {
	Sort orden = Sort.by(Sort.Direction.ASC, &quot;nombre&quot;);
	return repositorio.findAll(orden);
}

答案1

得分: 1

你可以使用 LinkedHashSet 和 JPA 注解 @OrderBy

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

You can use a LinkedHashSet + JPA annotation @OrderBy

@OrderBy
private Set&lt;MesSiembra&gt; mesesSiembra = new LinkedHashSet&lt;&gt;();

答案2

得分: 0

我已经将这个部分替换为:

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

在Spring Boot控制台中,我得到以下错误:

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'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is 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)]
英文:

I have replaced this:

@ManyToMany
private Set&lt;MesSiembra&gt; mesesSiembra;

For this:

@OrderBy(&quot;mesSiembra_id&quot;)
private Set&lt;MesSiembra&gt; mesesSiembra = new LinkedHashSet&lt;&gt;();

In the Spring Boot console I get the following error:

> 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'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is 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)]

huangapple
  • 本文由 发表于 2020年8月8日 23:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定