I'm implementing the repository structure of an entity which extends a MappedSuperClass in Sping data JPA.
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> {
}
When I try to save Base object in the following way:
@Autowired
BaseRepositoryImpl baseRepositoryImpl;
@Test
public void contextLoads() {
Base base = new BaseImpl();
base.setFirstName("Mamatha");
System.out.println(baseRepositoryImpl.save(base));
}
I do see a compile time error in the sysout line saying "The method save(Iterable<S>) in the type JpaRepository<BaseImpl,Long> is not applicable for the arguments (Base)"
.
In JPA, everything happens through MappedSuperClass only, except the instantiation. Am I going wrong in implementing the repository structure. Please help.