'Show OneToMany connection
I am trying in vain to display a list of objects of type calendar entries. I use a @OneToMany and @ManyToOne assertion. If I call up my controller with Postman, I "only" see my user, which also contains my entries. How can I display everything here without an extra query in a repository?
KalenderEintraege:
@JsonIgnore
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
User:
@JsonIgnore
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<KalenderEintraege> eintrage = new ArrayList<KalenderEintraege>();
Backend (RestController):
@GetMapping("/InsertInto")
public void einfuegen() {
User user = new User();
user.setName("Ben");
KalenderEintraege kalender = new KalenderEintraege();
kalender.setBeschreibung("Test");
kalender.setDatum(new Date());
kalender.setUser(user);
List<KalenderEintraege> listKalender = new ArrayList<KalenderEintraege>();
listKalender.add(kalender);
user.setEintrage(listKalender);
userRepro.save(user);
}
@GetMapping("/All")
public List<User> getUser() {
return (List<User>) userRepro.findAll();
}
Solution 1:[1]
Both directions were annotated with @JsonIgnore, so it won't display relational data, but will query it anyways, try activate "show sql" in console with these settings:
// add these in property file and you can see generated sql
spring.jpa.show-sql: true
spring.jpa.properties.hibernate.format_sql=true
However, getting rid of both @JsonIgnore will cause stackoverflow error, you can try @JsonIgnoreProperties annotation to solve it:
@JsonIgnoreProperties("user") // your Table name
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<KalenderEintraege> eintrage = new ArrayList<KalenderEintraege>();
@JsonIgnoreProperties("KalenderEintraege") // your Table name
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

