[英]QueryDSL add cross join when building predicate queries
我想從modelA中的modelA基礎上獲取數據。 但對於我將在modelB中記錄的每條記錄,並非強制性的。 因此,當我使用下面的代碼來獲取數據時,它返回0條記錄。
BooleanExpression searchCriteria = searchCriteria
.and(
qModelA.modelb.user.id.eq(userId)
)
.and ( some other conditions as well);
modelA.findAll(searchCriteria, pageable);
當我調試我發現QueryDsl放交叉連接。 任何人都可以告訴我如何解決這個問題,是否有任何方式querydsl添加左連接而不是交叉連接? 下面是我的兩個型號。
@Entity
public class ModelA implements Serializable {
private Long id;
private String label;
private ModelB modelB;
@Id
@SequenceGenerator(name = "modelASeq", sequenceName = "modela_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "modelASeq")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(length = 150, nullable = false)
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
@OneToOne(mappedBy = "modelA", cascade = CascadeType.REMOVE)
public ModelB getModelB() {
return modelB;
}
public void setModelB(ModelB modelB) {
this.modelB = modelB;
}
}
@Entity
public class ModelB implements Serializable {
private Long id;
private User user;
private ModelA modelA;
@Id
@SequenceGenerator(name = "modelBSeq", sequenceName = "modelb_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "modelBSeq")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@ManyToOne
@JoinColumn(name = "user_id", nullable = false )
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@NotNull
@OneToOne
@JoinColumn(name = "modela_id", nullable = false)
public ModelA getModelA() {
return modelA;
}
public void setModelA(ModelA modelA) {
this.modelA = modelA;
}
}
如果您需要左連接,則需要使用顯式連接:
query.from(modelA)
.leftJoin(modelA.modelB, modelB)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.