Correct creation of tables

Asked

Viewed 38 times

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 is with all the order fields, how can I fix ? I only want the fields of the Object class in the object table. Already 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 ?

1 answer

1


Hello,

for this case you must adopt the table by Subclass,do the following:

  • Add in the 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 issue of performance with many inheritances, which may affect the performance of your software.

  • Show, thank you very much.

Browser other questions tagged

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