java - 对 Spring Data JPA 和泛型类型感到困惑
问题描述
表:
StudentHistory 1--->n Student
TeacherHistory 1--->n Teacher
我尝试重新组合历史的 JPA 行为,因为它们做同样的事情(例如,从给定的历史中检索学生/老师)。
具有泛型类型的实体:
// Entities
public abstract class AbstractHistory <T> {}
public class StudentHistory extends AbstractHistory<Student> {}
public class TeacherHistory extends AbstractHistory<Teacher> {}
具有通用类型的存储库:
// repositories
public interface IHistoryRepository<T> extends CrudRepository<AbstractHistory<T>, Long> {
public AbstractHistory<T> findFirst();
}
public interface StudentHistoryRepository extends IHistoryRepository<Student> {}
public interface TeacherHistoryRepository extends IHistoryRepository<Teacher> {}
我虽然可以这样做:
StudentHistory stuHisto = new StudentHistoryRepository().findFirst();
但我得到这个错误:
// err -> Type mismatch: cannot convert from AbstractHistory<Student> to StudentHistory
1/ 为什么我不能从我的 'StudentHistoryRepository' 中检索 'StudentHistory' ?
2/ 我应该如何处理?
解决方案
您遇到此问题是因为您的方法显式返回 anAbstractHistory
而不是子类型。
- 你需要投...
- ...如果只有您的存储库实现了解每个 T 您都会获得特定的历史记录。
您可以尝试添加另一种类型,但我担心它会失败:
public interface IHistoryRepository<
T,
H extends AbstractHistory<T>
> extends CrudRepository<H, Long> {
public H findFirst();
}
public interface StudentHistoryRepository extends IHistoryRepository<Student, StudentHistory> {}
public interface TeacherHistoryRepository extends IHistoryRepository<Teacher, TeacherHistory> {}
我不知道您使用的是什么框架,可能是名称中的 Spring Data;虽然我过去使用过它,但我不知道它是否能够做到这一点。
毕竟,它需要获取具体类,并且由于它是泛型,类型擦除可能会干扰(如果有关表示 H 的具体类型的信息在反射中丢失,那么 Spring Data 可能无法在这里做很多事情,除非你用注释或其他东西帮助它)。
另一个应该可行的解决方案是每个子界面都这样做:
public interface StudentHistoryRepository extends CrudRepository<StudentHistory, Long> {
StudentHistory findFirst();
}
或者使用另一个界面:
public interface FindFirst<T> {
T findFirst();
}
public interface StudentHistoryRepository extends CrudRepository<StudentHistory, Long>, FindFirst<StudentHistory> {}
推荐阅读
- python - 每个标签聚合,当每行都有一个标签数组时
- java - 微服务没有从服务器获取配置:http://localhost:8888 Springboot
- python - MODIS AQUA 数据 - 使用 python GDAL 堆叠/马赛克数据
- php - ckeditor没有保存在mysql中
- c# - Reactive Observable.Create 为没有 Task.Run 的异步生产者
- python - 嵌套字典的交换键
- sql - SQL Server:CROSS APPLY 等效于 ROW_NUMBER() 查询
- python - 从 dicts descripton 的 python dict 创建加权 NetworkX DiGraph
- python - 在树莓派上运行两个线程的问题
- javascript - Javascript 日期字符串格式化