Answer the question
In order to leave comments, you need to log in
Entity relationships in JPA. Are two entities possible for one table?
I want such structure in java:
An abstract parent with a standard set of fields (id, name, etc). Entities are inherited from it and optionally add their fields.
In the database, it should look like this:
One common table for all entities inherited from Parent, and for those that have added their fields, separate tables with the InheritanceType.JOINED policy .
Those. for such a set of classes in the database there should be 2 tables:
parent and ch3 which has only id (link to the parent table) and extensionValue.
This is how an almost working scheme was added:
One abstract one with @MappedSuperclass, two abstract ones from it with different inheritance policies, and it is already final classes, but it doesn’t work exactly.
But it would be desirable without crutches.
Answer the question
In order to leave comments, you need to log in
Hello!
I recently started learning Spring Boot. And it's also interesting. And so I solved this issue as follows. In my case, these are 3 Entities - User, Customer, Author.
@Entity
@Data
@Table(name = "users")
@Inheritance(
strategy = InheritanceType.JOINED
)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
private String userEmail;
private String userPassword;
// getters setters etc.
}
@Entity
@Getter @Setter
@Table(name = "customers")
public class Customer extends User {
private String customerData;
}
@Entity
@Getter @Setter
@Table(name = "authors")
public class Author extends User {
private String authorData;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question