0

I have a "branche" that have many "categories" :

    @JsonProperty
    @OneToMany(
            cascade = CascadeType.ALL
        )
    @JoinColumn(name="categorie_id")
    @LazyCollection(LazyCollectionOption.FALSE)
    private Collection<Categorie> categorie=new ArrayList<>();
    @JsonProperty
    @ManyToOne
    @JoinColumn(name="branche_id")
    private Branche branche;

I want to list the categories with the branche associated ! this works fine but i think its not written well. I don't want to use mapped By on the @OneToMany , when i use it , my list of categories does no longer contain the "branche" associated .

Lamyae Lac
  • 43
  • 11

1 Answers1

0

I would try to do it like this

@JsonProperty
@OneToMany(
        cascade = CascadeType.ALL,
        mappedBy = "branche"
    )
private Collection<Categorie> categorie=new ArrayList<>();

And

  @JsonProperty
    @ManyToOne
    @JoinColumn(name="categorie_id")
    private Branche branche;

assuming that you have model that in which every branch can have only 1 shared category (and branch should contain id of category in category_id column)

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • But i want to display the branche when i display my categories list ! – Lamyae Lac Jun 22 '20 at 08:38
  • i tried it and i added JsonBackReference on ManyToOne and JsonManagedReference on OneToMany,but It still displays the categories on the branche ! – Lamyae Lac Jun 22 '20 at 08:48
  • categorie**s**? It should be only 1 category there. – Antoniossss Jun 22 '20 at 08:54
  • yes each branche have a lot of categories, sho when i try this ,and i call branches, it displays the branche and its categories, what i want is that when i call categories, is to diplay them with the branche asociated to each categorie. when i reverse the JsonBackReference with JsonManagedReference it works, but i think they should not be reversed, JsonBackReference should be on @ManyToOne and JsonManagedReference on OneToMany. – Lamyae Lac Jun 22 '20 at 09:01
  • if branch have multiple categories, and those categories can be attached to multiple branches, than its many-many. – Antoniossss Jun 22 '20 at 09:20
  • no each categorie have one branche, for ex : categorie 1-branche 1 ; categorie 2 -branche1 ; categorie 3-branche-2 ; categorie 4 - branche 3 ; categorie 5 - branche 3 – Lamyae Lac Jun 22 '20 at 09:26
  • @LamyaeLac Use `@JsonIdentityInfo` https://stackoverflow.com/a/20338702/4207306 – Eklavya Jun 22 '20 at 15:14