我正在使用spring数据,我提出了这个问题:
@Query("SELECT DISTINCT u.pk.fleet.fleetName FROM FleetHasUser u WHERE u.pk.user.username = ?1")
List<FleetName> allFleetNameForUser(String username);
此查询的目的是查找特定用户的所有FleetName。 这是数据库模式中感兴趣的部分:
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;
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吗?它是如何工作的(检查主键字段)?
我甚至可以创建像findDistinctPkFleetFleetNameByPkUserUsername
这样的Spring数据查询?
感谢
答案 0 :(得分:0)
据我所知,不在映射阶段的查询中应用了distinct,因为你可以看到@Query doc注释只执行jpql查询,因此本机不同。
正如你所说,执行一个独特的其他选项是 Spring data Distinct
你不能申请&#34;区别&#34;到对象,但你可以在Set上映射以避免重复,但更好的方法是正确过滤数据库上的数据。