首页 > 解决方案 > 对 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/ 我应该如何处理?

标签: javaspringgenericsspring-data-jpa

解决方案


您遇到此问题是因为您的方法显式返回 anAbstractHistory而不是子类型。

  1. 你需要投...
  2. ...如果只有您的存储库实现了解每个 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> {}

推荐阅读