gpt4 book ai didi

java - Spring 查询在整个对象上不同

转载 作者:行者123 更新时间:2023-11-30 22:11:04 26 4
gpt4 key购买 nike

我正在使用 spring 数据并进行了以下查询:

@Query("SELECT DISTINCT u.pk.fleet.fleetName FROM FleetHasUser u WHERE u.pk.user.username = ?1")
List<FleetName> allFleetNameForUser(String username);

此查询的目的是查找特定用户的所有 FleetName。这是感兴趣的数据库模式的一部分: enter image description here

FleetHasUser 类:

@Entity
@Table(name = "fleet_has_user", catalog = "dart")
@AssociationOverrides({
@AssociationOverride(name = "pk.user",
joinColumns = @JoinColumn(name = "id_username")),
@AssociationOverride(name = "pk.fleet",
joinColumns = @JoinColumn(name = "id_fleet")) })
public class FleetHasUser implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private FleetHasUserKeys pk = new FleetHasUserKeys();


@EmbeddedId
public FleetHasUserKeys getPk() {
return pk;
}

FleetHasUserKeys 类:

@Embeddable
public class FleetHasUserKeys implements Serializable {

private static final long serialVersionUID = 1L;
protected User user;
protected Fleet fleet;

舰队类:

@Entity
@Table(name = "fleet", catalog = "dart")
public class Fleet implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idFleet;
private Ecu ecu;
private String application;
private Cubic cubic;
private Integer power;
private String euroClass;
private String engineType;
private Traction traction;
private String transmission;
private String note;
private FleetName fleetName;
@JsonIgnore
private Set<Car> cars = new HashSet<Car>(0);
@JsonIgnore
private Set<FleetHasUser> fleetHasUsers = new HashSet<FleetHasUser>(0);

如您所见,FleetName 是一个类。我尝试不使用 distinct 并且列表中有重复的元素,因此在创建子查询之前我插入了 distinct 仅用于测试并且它有效。我没有在谷歌上找到信息,但是可以在对象上使用 distinct 吗?它是如何工作的(检查主键字段)?

也许我什至可以创建一个像 findDistinctPkFleetFleetNameByPkUserUsername 这样的 Spring 数据查询?谢谢

最佳答案

据我所知,distinct 应用于查询而不是映射阶段,如您所见@Query doc注释仅执行 jpql 查询,因此 native 不同。

正如您所说,执行不同的其他选项是 Spring data Distinct

您不能将“Distinct”应用于对象,但您可以映射到 Set 上以避免重复,但更好的方法是正确过滤数据库中的数据。

关于java - Spring 查询在整个对象上不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40102987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com