Mulgard Mulgard - 4 months ago 27
Java Question

Change value of abstract class in abstract repository

Im having a super class like the following:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class {

@GeneratedValue(strategy = GenerationType.AUTO)
protected long id = "";

protected String firstname = "";

protected String lastname = "";

protected Address address = null;

// getter setter

As you can see im using
ans the
and I want to keep it like that.

Now I got two subclasses inheriting from

public class Consulter extends Base {
// other fields

public class Customer extends Base {
// other fields

When I now create a repository like the following:

public interface Base_Repository extends JpaRepository<Base, Long> {


Will jpa be able to return the correct Object from the database when I provide the id of a concrete type? So something like that?

long id = 222;

Customer customer = customerRepository.findeOne(id);
Base base = baseRepository.findeOne(id);

boolean equals = customer.equals(((Customer) base));

And also would jpa be possible to do something like that?

Address address = new Address();
address.setStreet("space street 42");
address.setCountry("Trump Nation");;

Base base = baseRepository.findeOne(222);

Will this work or isnt this possible?


You cannot save an abstract entity because abstract entities aren't map to a database table. You need to save the child entity instead of the abstract one.