我正在实现一个实体的存储库结构,它在 Sping 数据 JPA 中扩展了 MappedSuperClass。
Base.java:
@MappedSuperclass
public abstract class Base {
public abstract Long getId();
public abstract void setId(Long id);
public abstract String getFirstName();
public abstract void setFirstName(String firstName);
}
BaseImpl.java:
@Entity
public class BaseImpl extends Base {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
...
//Default and parameterised constructors
//Getters and setters
//Equals and hashcode
//toString
}
BaseRepository.java:
@NoRepositoryBean
public interface BaseRepository<T extends Base, ID extends Serializable> extends JpaRepository<T, ID> {
}
BaseRepositoryImpl.java:
public interface BaseRepositoryImpl extends BaseRepository<BaseImpl, Long> {
}
当我尝试通过以下方式保存 Base 对象时:
@Autowired
BaseRepositoryImpl baseRepositoryImpl;
@Test
public void contextLoads() {
Base base = new BaseImpl();
base.setFirstName("Mamatha");
System.out.println(baseRepositoryImpl.save(base));
}
我确实在 sysout 行中看到了编译时错误"The method save(Iterable<S>) in the type JpaRepository<BaseImpl,Long> is not applicable for the arguments (Base)"
。在 JPA 中,除了实例化之外,一切都只通过 MappedSuperClass 发生。我在实现存储库结构时会出错吗?请帮忙。