i really don't know what actually my problem is.
I have two models in my Project.
model-package
- Ansprechpartner
- Lieferant
Ansprechpartner.java
@Entity
@Table(name = "ANSPRECHPARTNER")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"anlageAm", "updatedAt"}, allowGetters = true)
public class Ansprechpartner {
...
@NotNull
@ManyToOne
@JoinColumn(name = "lief_code", foreignKey=@ForeignKey(name = "APART_LIEF_FK"))
private Lieferanten liefCode;
public Lieferanten getLiefCode() {
return liefCode;
}
public void setLiefCode(Lieferanten liefCode) {
this.liefCode = liefCode;
}
...
}
Lieferant.java
@Entity
@Table(name = "LIEFERANTEN")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"anlageAm"}, allowGetters = true)
public class Lieferanten {
...
@Id
private String code;
@OneToMany(mappedBy = "liefCode")
private Set<Ansprechpartner> apart;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Set<Ansprechpartner> getApart() {
return apart;
}
public void setApart(Set<Ansprechpartner> apart) {
this.apart = apart;
}
...
}
My Controller:
@RestController
@RequestMapping("/apart")
public class AnsprechpartnerController {
...
@GetMapping("/all/{id}")
public Ansprechpartner getApartWithId(@PathVariable("id") long id) {
Ansprechpartner apart = apartRepository.findOne(id);
return apartRepository.findOne(id);
}
}
When i try to get the json data i get the following problem. Ansprechpartner gets data from Lieferant (because of that join). But then Lieferant again shows data from Ansprechpartner and so on.
Maybe better described with the following picture: Image with explanation
EDIT:
I finally solved it with the @JsonIgnoreProperties annotation:
In my Ansprechpartner.java i did it this way:
@NotNull
@JsonIgnoreProperties("apart")
// @JsonManagedReference
@ManyToOne
@JoinColumn(
name = "lief_code",
foreignKey=@ForeignKey(name = "APART_LIEF_FK")
)
private Lieferanten liefCode;
And in my Lieferanten.java i did it this way:
// @JsonBackReference
@JsonIgnoreProperties("liefCode")
@OneToMany(mappedBy = "liefCode", fetch = FetchType.LAZY)
private Set<Ansprechpartner> apart;