I have a problem regarding JPA inheritance in Spring.
This is the User Class which inherits to the Parent Class below:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class User implements Persistable<String> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
private String password;
private String username;
private String firstName;
private String lastName;
@Enumerated(EnumType.STRING)
private UserRole userRole;
And this is the Parent Class:
@Entity
public class Parent extends User{
private static final long serialVersionUID = 1L;
@NotNull
private String imgName;
@NotNull
private String location;
@NotNull
private String postcode;
@NotNull
private String streetName;
When I add a Parent object, User is still empty. Why? How do I make it so that User holds the parent object while using a unique ID? Do I insert Parent into User or Parent when adding an SQL statement in my data.sql?
Inheritance type TABLE_PER_CLASS means, that each entity has all its data, including inherited properties, in separate table. So when you add object of type Parent, all properties are stored in colums in table PARENT.
If you want to have properties from class User stored in table USER and properties from class Parent stored in table PARENT, you should use InheritanceType.JOINED. Rows with same ids are mapped to one object.
In case of inheritance type TABLE_PER_CLASS when you inserts Parent your sql statements insert into table PARENT only, but if you change to JOINED, one object should be inserted into both PARENT and USER.