【问题标题】:Left join Fetch Not working on multiple conditionsLeft join Fetch 不能在多个条件下工作
【发布时间】:2020-04-19 23:59:42
【问题描述】:

我正在写这个查询:

SELECT distinct g FROM Group g left join fetch g.groupPlaylists as gp on gp.playEndDay >= CURRENT_DATE and gp.status <>:status where g.zoneId= :zoneId and g.status <>:status

但是它抛出了一个异常:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: with-clause not allowed on fetched associations; use filters [SELECT distinct g FROM com.instoreradio.model.Group g left join fetch g.groupPlaylists as gp on gp.playEndDay >= CURRENT_DATE and gp.status <>:status where g.zoneId= :zoneId and g.status <>:status]

有没有办法解决这个问题?

它在没有 fetch 的情况下工作,但返回错误的 GroupPlaylist

这是我的实体映射: @实体 @Table(name = "组") @动态更新 公共类 Group 扩展 BaseModel {

private static final long serialVersionUID = -4864520840627628591L;

private String storeIds;

private Long regionId;

private Long zoneId;

private Long groupManagerId;

private String groupName;

private String groupComment;

@JsonBackReference
//@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name = "company_id")
private Companies companies;

@JsonManagedReference
@OneToMany(cascade=CascadeType.ALL,mappedBy="group",orphanRemoval=true)
@Fetch(FetchMode.JOIN)
private Set<GroupPlaylist> groupPlaylists=new HashSet<>();

}

@Entity
@Table(name = "group_playlist")
@DynamicUpdate
public class GroupPlaylist extends JsonType {

/**
 * 
 */
private static final long serialVersionUID = -982336326147846219L;

@JsonBackReference
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "group_id")
private Group group;

private Long groupCompanyId;

private String playlistTitle;

private Date playStartDay;

private Date playEndDay;

private Integer totalSongs;

private BooleanEnum isDefaultPlaylist;

@JsonManagedReference
@OneToMany(mappedBy = "groupPlaylist", fetch = FetchType.LAZY, 
orphanRemoval = true)
@Fetch(FetchMode.JOIN)
private Set<TimeSlot> timeSlots = new HashSet<>();

@JsonManagedReference
@OneToMany(mappedBy = "groupPlaylist", fetch = FetchType.LAZY, 
orphanRemoval = true)
@Fetch(FetchMode.JOIN)
private Set<StorePlaylist> storePlayLists = new HashSet<>();

@JsonManagedReference
@OneToMany(mappedBy = "groupPlaylist", fetch = FetchType.LAZY, 
orphanRemoval = true)
@Fetch(FetchMode.JOIN)
private Set<GroupPlaylistSongs> groupPlaylistSongs = new HashSet<>();

【问题讨论】:

  • 请出示您的组和播放列表的映射
  • 请看我也添加了实体

标签: hibernate spring-boot jpa spring-data


【解决方案1】:

您不能将 on 子句用于比映射中定义的更多条件。

您的查询必须使用 where 子句中的条件,如下所示:

SELECT distinct g 
FROM Group g 
left join fetch g.groupPlaylists as gp 
where gp.playEndDay >= CURRENT_DATE 
  and gp.status <>:status 
  and g.zoneId= :zoneId 
  and g.status <>:status

【讨论】:

  • 会返回错误的结果。如果某个组没有 GroupPlaylist,则该组将不会返回该组。
  • 但是在您的示例中您正在执行左连接。根据您的要求,您必须进行右外连接
猜你喜欢
  • 1970-01-01
  • 2018-11-01
  • 2013-01-11
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多