实体类的参数
@JoinEntity(joinEntityAlias = T_DeviceMoxing_AlarmRule,...)的作用
课室类
@Entity
public class Classroom {
private int id;
private String name;
...
}
老师类
注意其中的静态str
@Entity
public class Teacher {
public static final String T_Classroom = "T_Classroom";
private int id;
private String name;
@JoinEntity(name = "id",joinEntityClass = Classroom.class,joinEntityAlias =T_Classroom)
private int classroomId;
@Transient
@FieldPath(name = "name",joinEntityClass = Classroom.class,joinEntityAlias = T_DeviceMoxing_AlarmRule)
private String classroomIdName;
...
}
查询实体
注意其中的,joinEntityAlias
@JoinEntity(mainEnityClass = Teacher.class,joinEntityClass = Classroom.class, mainFiledName = "classroomId",name = "id",fieldType = Integer.class)
public class QueryBean {
//根据课室名模糊查询
@Like
@FieldPath(joinEntityClass = Classroom.class,name = "name",joinEntityAlias = Teacher.T_Classroom)
private String classroomIdName;
//用到的包
import swallow.framework.jpaquery.repository.annotations.FieldPath;
import swallow.framework.jpaquery.repository.annotations.JoinEntity;
import swallow.framework.jpaquery.repository.annotations.Like;
//根据课室名称模糊查询配备的教师数量
SELECT count
FROM
Teacher teacher
LEFT JOIN Classroom T_Classroom
WITH T_Classroom .Id = teacher.classroom_id
WHERE T_Classroom.name LIKE ?
如果在查询实体中的去掉
joinEntityAlias = Teacher.T_Classroom
的话,那么生成的sql就会报错,找不到Classroom.name字段
SELECT count
FROM
Teacher teacher
LEFT JOIN Classroom T_Classroom
WITH T_Classroom .Id = teacher.classroom_id
WHERE Classroom.name LIKE ?
刚开始使用时觉得joinEntityAlias没什么用就省略了,觉得很多余,后来才知道原来是这么用的。而且不加会导致级联查询也会有问题,只要会debug这些都好解决