Java Spring Jpa中多对多关系的查询
如果可能的话,我想用Spring数据Jpa进行多对多关系的查询(因为查询会很长)。这是我的实体:Java Spring Jpa中多对多关系的查询,java,spring,jpa,spring-data-jpa,Java,Spring,Jpa,Spring Data Jpa,如果可能的话,我想用Spring数据Jpa进行多对多关系的查询(因为查询会很长)。这是我的实体: @Entity public class Rental extends BaseEntity<Long> { private int price; private long clientId; private long gunTypeId; } 但我得到了一个错误: org.springframework.orm.hibernate5.HibernateSyst
@Entity
public class Rental extends BaseEntity<Long> {
private int price;
private long clientId;
private long gunTypeId;
}
但我得到了一个错误:
org.springframework.orm.hibernate5.HibernateSystemException:没有JDBC类型的方言映射:2002;嵌套的异常是org.hibernate.MappingException:JDBC类型没有方言映射:2002您可以尝试类似@Query(“选择r.gunTypeId,从Rentals中计数(r)按组(r.gunTypeId)按计数顺序(r)限制1”)的操作吗?这样我就有一个方言错误。我在问题中加了它,如果你能帮我的话
public GunType getMostRentedGunType() {
log.trace("getMostRentedGunType: --- method entered");
try {
Map<Long, Integer> count = new HashMap<>();
this.getAllRentals().forEach(rental -> {
count.put(rental.getGunTypeId(), count.getOrDefault(rental.getGunTypeId(), 0) + 1);
});
var max = count.entrySet().stream()
.max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1)
.get()
.getKey();
var result = gunTypeRepository.findById(max).orElse(null);
log.trace("getMostRentedGunType: result={}", result);
return result;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
@Query(value = "select gt from guntype gt where gt.id in (select r.guntypeid from rental r group by (r.guntypeid) order by count(r) desc limit 1)", nativeQuery = true)
GunType getMostRentedGunType();