Mulgard Mulgard - 17 days ago 9
Java Question

Change value of abstract class in abstract repository

Im having a super class like the following:

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

@Id
@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
TABLE_PER_CLASS
ans the
InheritanceType
and I want to keep it like that.

Now I got two subclasses inheriting from
Base


@Entity
public class Consulter extends Base {
// other fields
}

@Entity
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");

addressRepository.save(address);

Base base = baseRepository.findeOne(222);
base.setAddress(address);

baseRepository.save(base);


Will this work or isnt this possible?

Answer

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.

Comments