2012-05-31 79 views
1

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

我也試圖與

@QueryInit("*.*") 

標註在項目設置projectParticipants但是,這給出了同樣的異常。任何提示?

+1

你的實際查詢是怎樣的?我只看到謂詞的工廠方法。 –

+1

你可以發佈引發的異常,或者更好的堆棧跟蹤嗎?另外你的初始目標還不明確,如果你給我們一個你想要做什麼的想法,人們可以提供可能的選擇。 – siebz0r

回答

2

@TimoWestkämper @ siebZ0r

感謝您的關注。對不起,延遲迴復和錯誤措辭的問題。其實我想做的是寫一個有效的BooleanExpression。

與已經取得的註解組合,這是我是後:

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

我得到這個權利與同事的幫助。