Spring Data Repository泛型-尝试获取错误的类型

时间:2018-08-16 10:53:05

标签: java spring generics spring-data-jpa spring-data

抽象运动实体:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Exercise {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

}

抵抗运动实体:

@Entity
public class ResistanceExercise extends Exercise {
    ...
}

持续时间练习实体:

@Entity
public class DurationExercise extends Exercise {
    ...
}

抽象运动日志实体

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ExerciseLog<T extends Exercise> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @ManyToOne
    private T exercise;
}

抵抗运动日志实体:

@Entity
public class ResistanceExerciseLog extends ExerciseLog<ResistanceExercise> {

...

}

持续时间练习日志实体:

@Entity
public class DurationExerciseLog extends ExerciseLog<DurationExercise> {

    ... 
}

运动日志存储库:

public interface ExerciseLogRepository<T extends ExerciseLog<S>, S extends Exercise> extends JpaRepository<T, Long> {

}

控制器:

@RestController
@RequestMapping(value = "/api/v1/exercise-logs")
public class ExerciseLogController {

    @Autowired
    ExerciseLogRepository<ExerciseLog<Exercise>, Exercise> repository;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<List<ExerciseLog<Exercise>>> getLogs() {

        Pageable pageable = PageRequest.of(0, 20, Sort.unsorted());

        Page<ExerciseLog<Exercise>> pageResult = repository.findAll(pageable);
        return ResponseEntity.ok().body(pageResult.getContent());
    }       
}

通过上述设置,并存储了多种日志类型,在调用控制器的端点时,将在以下行中引发异常(完整堆栈跟踪here):

javax.persistence.EntityNotFoundException: Unable to find uk.deepblue.personaltrainer.domain.exercise.ResistanceExercise with id 8
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:159) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1240) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1123) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:464) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]

没有ID为8的ResistanceExercise,这是一个DurationExercise,并且仅从DurationExerciseLog表中进行引用,因此Spring / Hibernate似乎无法正确协商基础表。

我尝试了许多不同的配置,最终最终得到了相同的结果。看来TABLE_PER_CLASS继承策略是我要执行的操作(例如Using generics in Spring Data JPA repositories)的最佳选择,这是我目前的设置(如上所述)。

是否甚至可以使用多态查询和泛型来执行此操作,或者我必须为我拥有的每个ExerciseLog / Exercise组合进行调用?

1 个答案:

答案 0 :(得分:0)

感谢曼尼什(Manish)的建议,添加了targetEntity = Exercise.class已解决了此问题(尽管在此处更改为@MappedSuperclass是不合适的,因为这会阻止在其他情况下在其他地方使用Exercise实体)

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ExerciseLog<T extends Exercise> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @ManyToOne(targetEntity=Exercise.class)
    private T exercise;
}