具有多种类型的 CrudRepository 的不兼容类型
Incompatible types for CrudRepository with multiple types
我正在开发 Web 应用程序,您可以在其中管理学生和教师。这两个都是 类 并且有一个 Person
作为它们的超类。
我正在使用 Spring Boot,对于这两个 类 我正在使用 CrudRepository
来检索数据。两者都使用 Long
类型作为主键。
public interface StudentRepository extends CrudRepository<Student, Long> {
}
public interface TeacherRepository extends CrudRepository<Teacher, Long> {
}
我正在努力检索当前登录的用户,我想做这样的事情,所以我只需要调用一次 findOne
方法。
//Person
Person activePerson = activeUser.getPerson();
// Find the id from the active user
long activePersonId = activePerson.getId();
//Find the corresponding crudrepository
CrudRepository<Person, Long> repository = activePerson instanceof Student ? studentRepository : teacherRepository;
return repository.findOne(activePersonId);
使用这个时,出现错误:
Incompatible types. Required CrudRepository <Person, java.lang.Long>
. Found StudentRepository
Incompatible types. Required CrudRepository <Person, java.lang.Long>
. Found TeacherRepository
这是怎么来的,因为它们都有相同的超类?
为了我自己的尝试,我尝试从 repisitory
变量中删除通用标签,如下所示:
CrudRepository repository = activePerson instanceof Student ? studentRepository : teacherRepository;
return repository.findOne(activePersonId);
这也导致了类型不兼容的错误。
Incompatible types. Requried Person
. Found `java.
这是怎么来的,我怎样才能做到这一点,以便我可以为我想使用的特定 CrudRepository
创建一个通用变量?
问题是泛型不匹配。如果你只想允许 Person
的实现,你应该正确地将泛型写成 ? extends Person
:
CrudRepository<? extends Person, Long> repository = activePerson instanceof Student ? studentRepository : teacherRepository;
虽然我个人可能会写:
if (activePerson instanceof Student) {
return studentRepository.findOne(activePerson.getId());
} else {
return teacherRepository.findOne(activePerson.getId());
}
我正在开发 Web 应用程序,您可以在其中管理学生和教师。这两个都是 类 并且有一个 Person
作为它们的超类。
我正在使用 Spring Boot,对于这两个 类 我正在使用 CrudRepository
来检索数据。两者都使用 Long
类型作为主键。
public interface StudentRepository extends CrudRepository<Student, Long> {
}
public interface TeacherRepository extends CrudRepository<Teacher, Long> {
}
我正在努力检索当前登录的用户,我想做这样的事情,所以我只需要调用一次 findOne
方法。
//Person
Person activePerson = activeUser.getPerson();
// Find the id from the active user
long activePersonId = activePerson.getId();
//Find the corresponding crudrepository
CrudRepository<Person, Long> repository = activePerson instanceof Student ? studentRepository : teacherRepository;
return repository.findOne(activePersonId);
使用这个时,出现错误:
Incompatible types. Required
CrudRepository <Person, java.lang.Long>
. FoundStudentRepository
Incompatible types. Required
CrudRepository <Person, java.lang.Long>
. FoundTeacherRepository
这是怎么来的,因为它们都有相同的超类?
为了我自己的尝试,我尝试从 repisitory
变量中删除通用标签,如下所示:
CrudRepository repository = activePerson instanceof Student ? studentRepository : teacherRepository;
return repository.findOne(activePersonId);
这也导致了类型不兼容的错误。
Incompatible types. Requried
Person
. Found `java.
这是怎么来的,我怎样才能做到这一点,以便我可以为我想使用的特定 CrudRepository
创建一个通用变量?
问题是泛型不匹配。如果你只想允许 Person
的实现,你应该正确地将泛型写成 ? extends Person
:
CrudRepository<? extends Person, Long> repository = activePerson instanceof Student ? studentRepository : teacherRepository;
虽然我个人可能会写:
if (activePerson instanceof Student) {
return studentRepository.findOne(activePerson.getId());
} else {
return teacherRepository.findOne(activePerson.getId());
}