问题
Im having trouble using QueryDSL to filter with the below entities:
@Entity
@Table(name = "newidea")
@Cacheable(true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class NewIdea extends DomainObject implements Membership {
//id, other attributes etc
@OneToMany(fetch = FetchType.LAZY, mappedBy = "key.idea", cascade = CascadeType.ALL, orphanRemoval = true)
@QueryInit("newIdeaParticipations.key.student")
private Set<NewIdeaParticipation> newIdeaParticipations = new HashSet<NewIdeaParticipation>();
//constructor, getters, setters etc
}
@Entity
@Table(name = "newidea_student")
@AssociationOverrides({
@AssociationOverride(name = "key.student",
joinColumns = @JoinColumn(name = "role_id")),
@AssociationOverride(name = "key.idea",
joinColumns = @JoinColumn(name = "idea_id")) })
public class NewIdeaParticipation implements Serializable {
@EmbeddedId
@QueryInit("student")
private NewIdeaParticipationId key = new NewIdeaParticipationId();
//other attributes, constructor, getters, setters etc
}
@Embeddable
public class NewIdeaParticipationId implements Serializable {
@ManyToOne
private Student student;
@ManyToOne
private NewIdea idea;
}
The filters:
private BooleanExpression authorFilter(Student author){
// return QNewIdea.newIdea.newIdeaParticipations.any().key.student.eq(author); //NPE any is not null, but any.students attributes is null, and any.key.student is null
// return QNewIdea.newIdea.newIdeaParticipations.any().user.eq(author.getUser()); //hibernate.QueryException (any is null)
}
Top query exception:
http://pastebin.com/Vr8gxM7P
Bottom query exception:
http://pastebin.com/sdjMJcmx
What am I doing wrong? Am I missing something?
回答1:
The top query exception is caused by Querydsl's limited path initialization.
The path initialization is documented here http://www.querydsl.com/static/querydsl/3.2.2/reference/html/ch03s03.html#d0e1831
Concerning the second exception: Is the user property properly mapped?
回答2:
After updating to QueryDSL 3.2.3 the issue has been resolved and the below code works. (note the lack of @QueryInit annotations)
//idea class
public class NewIdea extends DomainObject implements Membership {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "key.idea", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<NewIdeaParticipation> newIdeaParticipations = new HashSet<NewIdeaParticipation>();
}
//participation class
@Entity
@Table(name = "newidea_student")
@AssociationOverrides({
@AssociationOverride(name = "key.student",
joinColumns = @JoinColumn(name = "role_id")),
@AssociationOverride(name = "key.idea",
joinColumns = @JoinColumn(name = "idea_id")) })
public class NewIdeaParticipation implements Serializable {
@EmbeddedId
private NewIdeaParticipationId key = new NewIdeaParticipationId();
}
//id class
@Embeddable
public class NewIdeaParticipationId implements Serializable {
@ManyToOne
private Student student;
@ManyToOne
private NewIdea idea;
}
//student class
public class Student extends Role {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "key.student", cascade=CascadeType.ALL, orphanRemoval=true)
private Set<NewIdeaParticipation> newIdeaParticipations = new HashSet<NewIdeaParticipation>();
}
//the filter
private BooleanExpression authorFilter(Student author) {
return QNewIdea.newIdea.newIdeaParticipations.any().key.student.eq(author);
}
来源:https://stackoverflow.com/questions/18347770/querydsl-filtering-on-set