Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
Spring: JPA Inheritance

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?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda