QueryDSL过滤集合

时间:2013-08-21 02:10:44

标签: java querydsl

我无法使用QueryDSL过滤以下实体:

@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;

}

过滤器:

    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)
    }

热门查询例外:

http://pastebin.com/Vr8gxM7P

底部查询例外:

http://pastebin.com/sdjMJcmx

我做错了什么?我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

最高查询异常是由Querydsl的有限路径初始化引起的。

此处记录路径初始化http://www.querydsl.com/static/querydsl/3.2.2/reference/html/ch03s03.html#d0e1831

关于第二个例外:用户属性是否已正确映射?

答案 1 :(得分:1)

更新到QueryDSL 3.2.3后,问题已解决,以下代码有效。 (注意缺少@QueryInit注释)

//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);
}