Correct creation of tables

2

Given my classes where Encomenda inherits from Objeto :

@Entity
@Table(name = "objeto")
@XmlRootElement(name = "objeto")
@XmlAccessorType(XmlAccessType.FIELD)
@AllArgsConstructor
@NoArgsConstructor
public class Objeto implements Serializable{

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Getter
    @Setter
    @Pattern(regexp = "[A-z]{2}\d{9}[A-z]{2}", message = "O código não é válido")
    @Size(min = 13, max = 13, message = "Apenas 13 caracteres")
    @Column(nullable = false, length = 13, unique = true)
    @NotBlank(message = "Nome não pode estar em branco")
    @XmlElement(name = "numero")
    private String codigo;

    @Getter
    @Setter
    @XmlElement(name = "evento")
    @OneToMany(mappedBy = "objeto", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Evento> eventos;

    @Getter
    @Setter
    @XmlElement
    @Column
    private String erro;

}

Order:

@Entity
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class Encomenda extends Objeto {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    @Column
    @XmlTransient
    private String local;

    @Getter
    @Setter
    @Column
    @XmlTransient
    private String descricao;

    @Getter
    @Setter
    @Column
    @XmlTransient
    private String evento;

    @Getter
    @Setter
    @Column
    @XmlTransient
    private String loja;

    @Getter
    @Setter
    @Column
    @XmlTransient
    private Timestamp horaEvento;

    @Getter
    @Setter
    @Column
    @XmlTransient
    private String situacao;

}

What is happening is that when tables are created in the database, the object table has all the order fields, how can I correct them? I want only the fields of the Object class in the object table. Now the order table I want fields of both classes, so far so good, what I need is to replicate the same values that are in object in the order fields, how to do?

    
asked by anonymous 07.04.2017 / 00:04

1 answer

1

Hello,

In this case you should adopt the table by SubClass, do the following:

  • Add in object class:  @Inheritance (strategy = InheritanceType.JOINED)

  • In the order class: @PrimaryKeyJoinColumn (name="id")

This way you will have separate tables. Be careful only with the performance issue with many inheritances, which may affect the performance of your software.

    
07.04.2017 / 21:51