简体   繁体   中英

Filter is not working on JPQL query of one to many relationship

I have created following classes ie Teacher and Student.

JAVA Code: Student

@Entity
@Table(name="Student")

public class Student {

    @Id
    @Column(name = "cmp_id")
    private String cmpId;

    @OneToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "teacherid", referencedColumnName = "teacherid")
    private Teacher teacher;

    @OneToOne
    @JoinColumn(name = "typeid", referencedColumnName = "typeid")
    private Type type;
}

JAVA Code: Teacher

@Entity
@Table(name="Teacher")
public class Teacher {

    @Id
    @Column(name = "teacherid")
    private String teacherid;

    @Column(name = "teachername")
    private String teachername;

    @OneToMany(mappedBy = "Teacher", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @OrderBy("cmpSequence ASC")
    private List<Student> std;
}

JAVA Code: Type

@Entity
@Table(name="Type")
public class Type{

    @Id
    @Column(name = "typeid")
    private Integer typeid;

    @Column(name = "uitype")
    private String uitype;    
}

Teacher is parent class and Student is child class.I want all teachers and their student with filter typeid is 1 but it does not work.

Following is the JPQL query which I created:

SELECT teacher FROM Teacher teacher WHERE teacher.std.typeid = 1

Its gives me following error:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Problem compiling query.The state field path 'teacher.std.typeid' cannot be resolved to a valid type.

Your query is wrong. You have Teacher which has std ( Student ), std has type ( Type ) and type has typeid field. So your query should looks like following:

SELECT teacher FROM Teacher teacher 
JOIN FETCH teacher.std AS s 
INNER JOIN s.type as t 
WHERE t.typeid = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM