Inheritance JPA Inheritance.JOINED does not work as it should

Asked

Viewed 225 times

1

Hello, I have the following classes Person, Personal and Personal. The last two classes share the same primary key of the class Person, that is, the primary key and foreign key of the tables Personal and Personal match the primary key of the table Person. Connect there in the diagram:

inserir a descrição da imagem aqui

Pessoa.java

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="pessoa")
public class Pessoa implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private Long idpessoa;

    @NotNull(message = "O atributo nome não pode ser nulo.")
    @Column(nullable=false, length=255)
    private String nome;
}

Personal Java.

@AllArgsConstructor
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name="pessoa_fisica")
public class PessoaFisica extends Pessoa {
    private static final long serialVersionUID = 1L;

    @Column(nullable = false, length = 14)
    private String cpf;

}

Personal.java.

@AllArgsConstructor
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name="pessoa_juridica")
public class PessoaJuridica extends Pessoa {
    private static final long serialVersionUID = 1L;

    @Column(nullable = false, length = 18)
    private String cnpj;


}

Everything should work like this, I persist a person in the bank. With that person’s id I need to associate her for either an individual or a legal person, that is, I can’t have a physical and legal person with the same id. At the time of saving and recovering a person everything ok, but if for example, to save a physical person happens an error saying that name can not be null. This is confusing since the concept of InheritanceType.JOINED each class should only persist its own attributes, the only thing shared is the primary key. Am I wrong? The only thing I want is to save cpf, idpessoa, only attributes belonging to the subclass and its entity.

  • Could share the error you are getting on the console?

  • "javax.validation.ConstraintViolationException: Validation failed for classes ... during persist time for groups [javax.validation.groups.Default, ]\nList of constraint violations:[\n\tConstraintViolationImpl{interpolatedMessage='O atributo nome não pode ser nulo.', propertyPath=nome, rootBeanClass=class @Erickluz

  • 3 years ago I answered a similar question, see if it works. https://answall.com/questions/43139/heran%C3%A7a-com-jpa

  • @Andrémartins TABLE_PER_CLASS does not work for the purpose of this implementation, I need to generate IDENTITY keys, which TABLE_PER_CLASS does not allow

1 answer

1


All solution was reported in the comments:

Pessoa.java

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="pessoa")
public abstract class Pessoa implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private Long idpessoa;

    @NotNull(message = "O atributo nome não pode ser nulo.")
    @Column(nullable=false, length=255)
    private String nome;
}

Personal Java.

@AllArgsConstructor
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name="pessoa_fisica")
public class PessoaFisica extends Pessoa {
    private static final long serialVersionUID = 1L;

    @Column(nullable = false, length = 14)
    private String cpf;

}

Personal.java.

@AllArgsConstructor
@NoArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name="pessoa_juridica")
public class PessoaJuridica extends Pessoa {
    private static final long serialVersionUID = 1L;

    @Column(nullable = false, length = 18)
    private String cnpj;

}

The class Pessoa.java is available for requests GET and DELETE. But being Abstract is impossible to instantiate an object through a POST. To instantiate a physical person I need to put the attributes of person, as name and the rest of the attributes of the physical person class itself. So the same is true for the legal person. So there is no natural or legal person who has the same id.

  • I’m still getting the same error. Now I’m thinking that the problem should exist with the use of Lombok...

  • How are you testing data insertion? Junit? main? jsf? method etc..

  • Requisition POST, a JSON body. { "cpf": "11111111111", "idpessoa": 1 }

  • Try placing the 'name' field on this json, if the error persists implements the toString() method in the child class, and put here the print of what appears on the console in System.out.println(obj.toString());

  • Hey, man, I got the problem solved. I made the class Pessoa asbtrata, and the only thing I do is a POST JSON request body for an individual or a legal person without having to pass the "idpessoa", because at the time of the POST the id is generated in person and inherited to the type of person being persisted at the time, so we can’t have the same id for a legal person at the same time.

  • And now it makes a lot of sense to have the Person class as abstract since it doesn’t have to be a resource in my API (/people for example), just a class that helps me manage common ids and attributes for types of people on the database side. If I make one SELECT * FROM pessoa I list all natural and legal persons.

  • Would you like to have the honor of editing the answer? Just put abstract in Person and delete the builders. Do this and reference to my comments here, I will give check. Thanks and much for the help there the/

  • Ahh understood, but even for me to understand, you made it work without the builders? I mean if you take off/off, keep working?

  • I didn’t use the builders no, I let Lombok take care of it... and out of curiosity and happiness I’m managing to use the person class as a resource of my API.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.