1

我在使用 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

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

4

2 回答 2

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);
}
于 2013-09-27T05:03:55.857 回答
1

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

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

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

于 2013-08-21T18:19:00.680 回答