Generating correct json with p-dropdown primeng?

0

I'm new to Angular and I have the following situation using the API in java:

I have the "Launch" class where the NOT category is mandatory (Category class)

@Entity
@Table(name = "lancamento")
public class Lancamento {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long codigo;

    @NotNull
    private String descricao;

    @ManyToOne
    @JoinColumn(name = "codigo_categoria")
    private Categoria categoria;

    (...)
}

/ p>
{"descricao":"teste","categoria":{}}

But my API returns the following error (in java) due to not having reported the category:

TransientObjectException - object references an unsaved transient instance - save the transient instance before flushing

I ran a Postman test by submitting the JSON as follows to register the release:

{"descricao":"teste"}

In this way I can register, then there is the doubt:

The error is in the angle that is using the component p-dropdown of primeng as follows:

    <p-dropdown placeholder="Selecione..." [autoWidth]="false"
      [filter]="true" [options]="categorias"
      [(ngModel)]="lancamento.categoria.codigo" name="categoria"
      #categoria="ngModel"></p-dropdown>

Or is the error in the category declaration in the class class?

When you register the release by reporting the category, there are no errors and JSON is sent as follows:

{"descricao":"teste","categoria":{"codigo":1}}
    
asked by anonymous 02.11.2018 / 04:33

1 answer

0

Bruno, I think his error is related to Java and even is very common in Many-to-many relationships, a good article is this link . There are two possible workarounds: use cascade in the relationship ( cascade=CascadeType.PERSIST ) or persist all new objects separately.

    
05.11.2018 / 16:47