Spring数据。覆盖通用接口方法

时间:2017-05-29 07:45:17

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

我已经检查了如何覆盖泛型。考虑CrudRepository我可以覆盖:

<S extends T> S save(S var1);

在我的存储库中:

public interface ChannelDetailsRepository extends IgniteRepository<ChannelDetails, Long> {
  @Override
  default ChannelDetails save(ChannelDetails entity) {
    throw new UnsupportedOperationException();
  }
...
}

但有些情况尚不清楚:

<S extends T> Iterable<S> save(Iterable<S> var1);

我尝试过使用(在ChannelDetailsRepository界面中)

@Override
default Iterable<ChannelDetails> save(Iterable<ChannelDetails> entities) {
    throw new UnsupportedOperationException();
}

我的尝试因编译错误而失败:

Error:(28, 38) java: name clash: save(java.lang.Iterable<com.nextiva.calendar.entity.ChannelDetails>) in com.nextiva.calendar.dao.ignite.ChannelDetailsRepository and <S>save(java.lang.Iterable<S>) in org.springframework.data.repository.CrudRepository have the same erasure, yet neither overrides the other
Error:(26, 5) java: method does not override or implement a method from a supertype

并且我不知道如何修复此通用覆盖。

1 个答案:

答案 0 :(得分:3)

您需要保留其他类型参数S,以便该方法接受并分别返回T ChannelDetails的子类型。

@Override
default <S extends ChannelDetails> Iterable<S> save(Iterable<S> entities) {
    throw new UnsupportedOperationException();
}