本文介绍了QueryDSL 过滤设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 QueryDSL 过滤以下实体时遇到问题:

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;

}

过滤器:

    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

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

What am I doing wrong? Am I missing something?

推荐答案

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

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

这篇关于QueryDSL 过滤设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 14:47