QueryDSL adds cross join when building predicate queries

I want to get data from modelA database for users who are in modelB. But it is not necessary that for each record I will have a record in modelB. Therefore, when I use the code below to retrieve data, it returns 0 records.

BooleanExpression searchCriteria = searchCriteria .and( qModelA.modelb.user.id.eq(userId) ) .and ( some other conditions as well); modelA.findAll(searchCriteria, pageable); 

When I debug, I found that QueryDsl put Cross join. Can someone tell me how to fix this, is there a way that querydsl adds a left join instead of a cross join? Below are two of my models.

 @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; } } 
+5
source share
1 answer

If you need a left join, you will need to use explicit joins:

 query.from(modelA) .leftJoin(modelA.modelB, modelB) ... 
0
source

All Articles