M
M
Mikhail Ketov2019-03-07 21:37:26
Java
Mikhail Ketov, 2019-03-07 21:37:26

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 .
5c816452a05fc787999345.png
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

1 answer(s)
O
Orkhan, 2019-03-07
Hasanly @azerphoenix

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;
}

And in this case, you will get 3 tables. One common - User + 2 tables for Customer & Author, in which there will be an ID from the Entity User + additional. fields from child tables. Did you mean it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question