0

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.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
User1230321
  • 1,435
  • 4
  • 23
  • 39
  • You are persisting an entity which is of type `BaseImpl` using a repository which takes `BaseDataImpl` as entities!! Please also post your `BaseDataImpl` Entity. – Abdullah Khan Dec 15 '16 at 03:06
  • The provided code doesn't compile so there is no question of a runtime error. `BaseRepositoryImpl.save` expects a `BaseImpl` whereas it is being passed a `Base` instance. – manish Dec 15 '16 at 05:04
  • @manish: Yes, it is a compile time error only. Not the runtime one. Have updated the question. – User1230321 Dec 15 '16 at 05:23
  • @AbdullahWasi: Actually, BaseDataImpl was a mistake, It should have been BaseImpl only. Have updated the question. Sorry for that. – User1230321 Dec 15 '16 at 05:29
  • Your question is unclear. Are you asking why you are getting a compile-time error (there is a type mismatch), how to fix it (pass the correct type to the `save` method) or something else? – manish Dec 15 '16 at 05:30
  • @manish: I would want everything using Base only, the way we do it in JPA. I cannot use the BaseRepository object to save, because it will be a no repository bean class. – User1230321 Dec 15 '16 at 05:31
  • 1
    You can have `BaseImpl something = new BaseImpl(); something.setFirstName(...); Base base = baseRepositoryImpl.save(something);`. You can then use the `Base` instance returned by the `save` method instead of passing a `Base` instance to it, which is not possible due to type-safety. – manish Dec 15 '16 at 05:35
  • Well, that means I can't send a mappedSuperClass object at all to the save function. In JPA, we can send a mappedSuperClass object to save/persist function. Yes, what returns is a MappedSuperClass object. – User1230321 Dec 15 '16 at 06:04
  • 1
    " In JPA, we can send a mappedSuperClass object to save/persist function." Really? I think you're mistaken. https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Cannot_query.2C_persist.2C_or_have_relationships – Alan Hay Dec 15 '16 at 13:44

0 Answers0