与 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/ 我应该如何处理?


九州编程
浏览 209回答 1
1回答

至尊宝的传说

你有这个问题,因为你的方法显式返回一个AbstractHistory而不是子类型。你需要投......如果只有您的存储库实现理解每个 T 您都会获得特定的历史记录。您可以尝试添加另一种类型,但我担心它会失败:public interface IHistoryRepository<&nbsp; T,&nbsp; H extends AbstractHistory<T>> extends CrudRepository<H, Long> {&nbsp; &nbsp; public H findFirst();}&nbsp; &nbsp;&nbsp;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> {&nbsp; StudentHistory findFirst();}或者使用另一个接口:&nbsp; public interface FindFirst<T> {&nbsp; &nbsp; T findFirst();&nbsp; }&nbsp; public interface StudentHistoryRepository extends CrudRepository<StudentHistory, Long>, FindFirst<StudentHistory> {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java