我创建了以下课程,即老师和学生。
@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;
}
@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;
}
@Entity
@Table(name="Type")
public class Type{
@Id
@Column(name = "typeid")
private Integer typeid;
@Column(name = "uitype")
private String uitype;
}
老师是家长班,学生是小学班。我希望所有老师和他们的学生使用过滤器typeid是1,但它不起作用。
SELECT teacher FROM Teacher teacher WHERE teacher.std.typeid = 1
java.lang.IllegalArgumentException:发生异常时 在EntityManager中创建查询:异常描述:问题 编译查询。状态字段路径&#39; teacher.std.typeid&#39;不可能是 解析为有效类型。
答案 0 :(得分:3)
您的查询错误。你有Teacher
std(Student
),std有类型(Type
),类型有typeid字段。因此,您的查询应如下所示:
SELECT teacher FROM Teacher teacher
JOIN FETCH teacher.std AS s
INNER JOIN s.type as t
WHERE t.typeid = 1