Save daughter entity against Manytoone Hibernate Jpa

Asked

Viewed 75 times

0

I have 3 entities: Request, Request and Service; There is a many-to-many relationship between the Request and Service entities so I created the third table;

I would like to save a Request and also the children;

What happens is you’re saving the order normally but you don’t save the kids!

Below is an example of the code!

Service Class

@Data
@Entity
@Table(name = "servico")
public class Servico {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_servico")
    private long idServico;
    @Column(name = "nome_servico", length = 100, nullable = false)
    private String nomeServico;
    @Column(name = "valor_hora", nullable = false)
    private double valorHora;
}

Request Class

@Data
@Entity
@Table(name = "pedido")
public class Pedido {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_pedido")
    private Long idPedido;
    @Column(name = "valor_total_bruto", nullable = false)
    private double valorTotalBruto;
    @Column(name = "valor_total_liquido", nullable = false)
    private double valorTotalLiquido;
    @Column(name = "perc_imposto", nullable = false)
    private double percentualImposto;
    @Column(name = "usuario", length = 100, nullable = false)
    private String usuario;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "pedido")
    private List<PedidoServico> pedidoServicos;
}

Class Pedido_servico

@Data
@Entity
@Table(name = "pedido_servico")
public class PedidoServico {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_pedido_servico")
    private long idPedidoServico;
    @Column(name = "qtd_hora", nullable = false)
    private int qtdHora;

    @ManyToOne()
    @JoinColumn(name = "id_pedido")
    private Pedido pedido;

    @ManyToOne()
    @JsonIgnore
    @JoinColumn(name = "id_servico")
    private Servico servico;
}

This way saving the Order and the Request Service, the problem is that in the "intermediate" class is not saving the Foreign key: id_service and id_request! How can I solve this problem?

Segment image of the test!

inserir a descrição da imagem aqui

Bank score

inserir a descrição da imagem aqui

  • in manyToMany you will have a pivot table that when it is defined in Join is already created then just pass the data in body https://www.baeldung.com/jpa-many-to-many

  • Hello André thanks for the answer, that way would work smoothly, in my case I need to save more information in the associative entity besides the 2 Foreign key, but after a lot of research I managed to solve hehe :D

1 answer

1

I figured it out that way. In the Request class the relationship was like this:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id_pedido")
private List<PedidoServico> pedidoServicos = new ArrayList<>();

and in the requested class:

@ManyToOne()
@JoinColumn(name = "id_servico", nullable = false)
private Servico servico;

Browser other questions tagged

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