我实现了在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发生。我在实现存储库结构时出错了吗?请帮忙。