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:
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?
– Erick Luz
"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– vanilton.alv3sfilho
3 years ago I answered a similar question, see if it works. https://answall.com/questions/43139/heran%C3%A7a-com-jpa
– André Martins
@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
– vanilton.alv3sfilho