英文:
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 = "especie_mesesSiembra"
,joinColumns=@JoinColumn(name="especie_id")
,inverseJoinColumns=@JoinColumn(name="mesSiembra_id")
)
@ManyToMany
private Set<MesSiembra> mesesSiembra;
}
Class EspecieControlador (Controller)
@GetMapping
public List<Especie> listarTodos() {
return servicio.listarTodos();
}
Class EspecieServicio (Service)
public List<Especie> listarTodos() {
Sort orden = Sort.by(Sort.Direction.ASC, "nombre");
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<MesSiembra> mesesSiembra = new LinkedHashSet<>();
答案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<MesSiembra> mesesSiembra;
For this:
@OrderBy("mesSiembra_id")
private Set<MesSiembra> mesesSiembra = new LinkedHashSet<>();
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)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论