QueryDSL查询异常

时间:2012-05-31 03:17:02

标签: java hibernate jpa querydsl

我遇到QueryDSL查询问题。类:

@Entity
@Table(name="project")
@Cacheable(true)
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Project extends DomainObject implements Comparable<Project>, IconizedComponent, Commentable {

    @ManyToMany(targetEntity=Student.class)
    @JoinTable(name="project_student")
    @Sort(type=SortType.NATURAL) //Required by hibernate
        @QueryInit({"user"})
    private SortedSet<Student> projectParticipants = new TreeSet<Student>();

    private Project(){}

    //attributes, get+set methods etc

}

@Entity
@Cacheable(true)
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) //Hibernate specific
public class Student extends Role {

    public Student(){}

    //attributes, get+set methods etc

}

@Entity
@DiscriminatorColumn(name = "rolename", discriminatorType = DiscriminatorType.STRING, length = 8)
@Table(name="role", uniqueConstraints={@UniqueConstraint(columnNames={"user_id","rolename"}, name = "role_is_unique")})
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class Role extends LazyDeletableDomainObject implements Comparable<Role> {

    @ManyToOne(optional=false)
    protected User user;

    public Role(){}

    //attributes, get+set methods etc
}

@Entity
@Table(name="user")
@Cacheable(true)
@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) //Hibernate specific
public class User extends LazyDeletableDomainObject implements Comparable<User>, IconizedComponent {

    private String firstName;
    private String lastName;

    public User(){}

    //attributes, get+set methods etc

}

查询:

private BooleanExpression authorsNameContains(String searchTerm){
        QUser user = new QUser("user");
        user.firstName.containsIgnoreCase(searchTerm).or(user.lastName.contains(searchTerm));
        QStudent student = new QStudent("student");
        student.user.eq(user);
        return QProject.project.projectParticipants.contains(student);

    //java.lang.IllegalArgumentException: Undeclared path 'student'. Add this path as a source to the query to be able to reference it.
}

我还尝试使用

注释Project中设置的projectParticipants
@QueryInit("*.*")

但是这给出了同样的例外。任何提示?

1 个答案:

答案 0 :(得分:2)

@TimWestkämper @ siebZ0r

感谢您的关注。对不起延迟回复和错误措辞问题。实际上我想做的是编写一个有效的BooleanExpression。

结合已经制作的注释,这就是我所追求的:

private BooleanExpression authorsFirstNameContains(String searchTerm){
    return QProject.project.projectParticipants.any().user.firstName.containsIgnoreCase(searchTerm);
}

我在同事的帮助下做到了这一点。