fred
fred

Reputation: 1822

QueryDSL query exception

I have a problem with a QueryDSL query. Classes:

@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

}

Query:

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

I have also tried annotating the projectParticipants set in Project with

@QueryInit("*.*")

But that gives the same exception. Any hints?

Upvotes: 1

Views: 4063

Answers (1)

fred
fred

Reputation: 1822

@Timo Westkämper @siebZ0r

Thanks for your attention. Sorry for the delayed reply and incorrectly phrased question. Actually what I wanted to do was to write a working BooleanExpression.

In combination with the annotations already made, this was what I was after:

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

I got this right with the help of a colleague.

Upvotes: 2

Related Questions