0

Here is my project structure:
An @MappedSuperclass base class:

@MappedSuperclass
public class BaseClass {

   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
   private long id;
   //getter and setter
}

An @Entity extending the base class:

@Entity
public class Person extends BaseClass {

  private String regisNumber;
  private String name;
  private int hPerWeek;

  /**
   * @param regisNumber
   * @param name
   * @param hPerWeek
   */
  public Person(String regisNumber, String name, int hPerWeek) {
      super();
      this.regisNumber = regisNumber;
      this.name = name;
      this.hPerWeek = hPerWeek;
  }
  //getters and setters
}

The generic DAO:

    @Repository
    public interface IDao<T extends BaseClass> extends JpaRepository<T, Long> {
   }

In my tests, creating a Person works fine:

@Autowired
IDao<Person> dao;

@Test
public void whenPersonEntityIsCreated_thenNoExceptions() {
    Person person = new Person("mkd90ii", "manu", 24);      
    dao.save(person);

}

Nevertheless trying getting a Person :

@Test
public void whenPersonEntityIsUpdated_thenNoExceptions() {
    Person person = dao.getOne(Long.valueOf(32768));
    System.out.println(person.toString());
    //Updating person...

}

generates me this error:

   org.springframework.dao.InvalidDataAccessApiUsageException: Unknown   entity: com.bockmoi.entities.BaseClass;   
     nested exception is java.lang.IllegalArgumentException: Unknown  entity: com.bockmoi.entities.BaseClass

I do understand that's because BaseClass is not a javax.persistence.Entity, but why the creation works and not the reading?

Can someone explain me why this happens and how to overcome this?
It's a kind of dead end for me.
Thanks

Philippe Simo
  • 1,353
  • 1
  • 15
  • 28
  • Did you make entity manager to scan all entities? Looks like this BaseClass is not managed – Nagaraddi Dec 14 '16 at 09:18
  • why is there the hibernate tag ? as mentioned by @nagareddy , probably an issue with your configuration , but still aint clear why save works. Have you confirmed that save works ? Have you checked the database and you can see the row ? A related issue might be this one , check this [answer](http://stackoverflow.com/questions/27545276/how-to-implement-a-spring-data-repository-for-a-mappedsuperclass) – AntJavaDev Dec 14 '16 at 09:23
  • @AntJavaDev save works, and indeed i checked the database and i saw the registered raw. So the Baseclass is managed. this [question](http://stackoverflow.com/questions/22971966/jpa-spring-data-cant-get-or-delete-persisted-objects) is related to mine but has no accepted answer too – Philippe Simo Dec 14 '16 at 10:00
  • @AntJavaDev spring-data-jpa is not a JPA provider but relies on a JPA provider: Hibernate in this case. – Philippe Simo Dec 14 '16 at 10:03
  • Well Spring Data will configure that for you , no matter which provider you choose(by default the spring-boot-starter-data is importing hibernate). In your case you havent mentioned at all that you are using hibernate's entity manager underneath, but simply added the tag . Anyways , that means you have configured an entityManager as well ? Just for testing , could you perform the same tests but dont access the Spring's repository and go directly with the hibernate entityManager , just to check that there is not any issue with your mapped entities – AntJavaDev Dec 14 '16 at 11:42
  • Also even if you manage to got it working with the entity manager , still wont mean anything , cause i've found a more related answer to your problem , @MappedSuperClass , might not work with Spring Data , check [here](http://stackoverflow.com/questions/25237664/use-abstract-super-class-as-parameter-to-spring-data-repository). You ll need to place a Discriminator Column – AntJavaDev Dec 14 '16 at 12:08
  • or else (sorry for the long comments) , you ll have to make the `IDao` as abstract and extend it for every Entity that extends `BaseClass`. In your case , you would need a PersonDao , smth like : `public interface PersonDao extends IDao` – AntJavaDev Dec 14 '16 at 12:29
  • @AntJavaDev that is what i was trying to avoid, anyway, based on what i found from different Q&A about this subject, i defenitely have to create specific extending dao(repositories) for each entity... – Philippe Simo Dec 14 '16 at 13:05
  • yeap otherwise pass it over hibernate and create an abstract service there . Still it wont work for select , as you have to define which entity you want to fetch – AntJavaDev Dec 14 '16 at 13:17

0 Answers0