Manytomany jpa - Spring Save Relationship Entity Data

Asked

Viewed 47 times

0

Good night, you guys! I have 2 classes: Services and Order. Services contain name and value/time. Request contains attributes: valueBrut, valueLike, percentualTime , user and a list of services.

made a relation Many to Many generating a 3° requested table servicos;

I’m doing based on this exercise:

The system must have a page to register the Services (service name and time value for sale); The system must have a page to register the Service Request, where it must be informed: (s) service(s) to be marketed(s); number of hours of each service; the professional who will be allocated to execute this Service Request; The % sales tax that will be added to the final order amount. When saving the Order, display in a dialog box showing the total order value and the % profit of that order for the company.

the question is, how do I add more fields in the requested table?

Example: object I imagine to save is similar to something like this:

{
   valorBruto: 50,
   valorLiquido: 40,
   percentualImposto: 10,
   usuario: "teste",
   imposto: 10,
   servicos: [{
        idServico: 1,
        qtdHora: 40
   }]
}

The problem is that when I try to save the request, it tries to save the requested servic_relationship that has the qtde_time attribute and this null and error occurs.

Follows the code of the classes.

Request

@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;

    @ManyToMany
    @JoinTable(name = "pedido_servico", joinColumns = { @JoinColumn(name = "id_pedido") },             
         inverseJoinColumns = {@JoinColumn(name = "id_servico") })
    private List<Servico> servicos;
}

Service

@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;
}

Beggar

@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;
    @Column(name = "id_servico", nullable = false)
    private long idServico;
}

1 answer

0

Good evening Fabio ! From what I understand the problem in this case you do not need this third class "Pedidoservico". Another detail in case would be a request to have several ne ? service in case you should use @Onetomany

@OneToMany
@JoinColumn(name="servicos_id", nullable=false)
private List<Servico> servicos;

One last detail in the Services class the serviceName field is nulllable=false, so you must pass a value, or change to nullable=true

Browser other questions tagged

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