【发布时间】:2021-08-31 12:41:23
【问题描述】:
我需要查询子类的属性。以下是涉及的实体:
@Entity
@Data
public class Course {
@Id
private Long id;
@ManyToOne
@JoinColumn(name = "personId")
private Person person;
}
@Entity
@Data
@Inheritance(strategy = InheritanceType.JOINED)
public class Person {
@Id
private Long id;
private String name;
}
@Entity
@Data
@EqualsAndHashCode(callSuper=true)
public class Student extends Person {
@ManyToOne
@JoinColumn(name="studentTypeId")
private StudentType studentType;
}
@Entity
@Data
public class StudentType {
@Id
private Long id;
@Convert(converter = StudentTypeConverter.class)
private StudentTypeEnum name;
@RequiredArgsConstructor
@Getter
@ToString
public static enum StudentTypeEnum {
GRADUATE("Graduate");
private final String name;
}
}
我正在尝试使用 Spring Data 存储库进行查询:
public interface CourseRepository extends Repository<Course> {
List<Course> findByCoursePersonStudentTypeName(@Param("studentTypeName") String studentTypeName);
}
但这不起作用。它会生成一个错误声明No property StudentTypeName found for Person!。这是有道理的,因为该属性仅存在于 Student。
如何编写此方法(最好)或使用 JPQL 按学生类型查找课程?
【问题讨论】:
-
课程中的人应该是学生吗?
-
@pirho - 不。就我而言,它通常是学生,但我需要其他类型的人参加的灵活性。
-
为什么要用
StudentType包裹StudentTypeEnum?StudentTypeConverter的作用是什么,有什么作用?是不是可以使用枚举name作为字符串?
标签: hibernate jpa spring-data-jpa spring-data jpql