0

I am using a Spring MVC Rest Controller to perform some simple CRUD operations on some entities. I'm am struggling a little with Jackson trying to figure out how to properly implement a @ManyToOne relationship. When Jacskon serializes the "inverse" side of the relationship, it omits the "owning" property. Heres a look at my code:

@Entity
public class Competition {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

private String nameInUrl;

@ManyToOne(cascade = CascadeType.ALL)
@JsonIdentityReference(alwaysAsId=true)
@JsonBackReference(value="test")
private Sport sport;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNameInUrl() {
    return nameInUrl;
}

public void setNameInUrl(String nameInUrl) {
    this.nameInUrl = nameInUrl;
}

public Sport getSport() {
    return sport;
}

public void setSport(Sport sport) {
    this.sport = sport;
}
}

@Entity
public class Sport {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;

private String nameInUrl;
@OneToMany(cascade = CascadeType.ALL, mappedBy="sport")
@JsonManagedReference(value="test")
private Set<Competition> competitions = new HashSet<Competition>();

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNameInUrl() {
    return nameInUrl;
}

public void setNameInUrl(String nameInUrl) {
    this.nameInUrl = nameInUrl;
}

public Set<Competition> getCompetitions() {
    return competitions;
}

public void setCompetitions(Set<Competition> competitions) {
    this.competitions = competitions;
}
}

I would like to ask how I can achieve serializing the Competition entity in its entirety, without omitting the Sport entity?

Thanks, Dave

DMcg
  • 129
  • 3
  • 11
  • Did you try serialize this objects? – ZhenyaM Apr 06 '17 at 21:30
  • Yes, I did. The json representation of the objects deserialize as expected but when I serialize the competition object the sport property is omitted??? – DMcg Apr 07 '17 at 19:09

1 Answers1

1

Well, according to this answer (if I understand right), @JsonBackReference and @JsonManagedReference pair in serialization don't implies store whole object in json, just id. But when you deserialize this json you will obtain recursive connections without StackOverFlow. So it expected behavior (I think=)).

But if you need to pass in json whole objects, than you need to remove @JsonBackReference and @JsonManagedReference and pass @JsonIgnore for competitions field in Sport class, but you will lost recursive connection in deserialize.

Also, you can try to write your own deserializer for Sport class for recursion obtain.

I hope this will help you.

Community
  • 1
  • 1
ZhenyaM
  • 676
  • 1
  • 5
  • 15
  • Yea, it does seem like the expected behaviour of the Jackson annotations. Just took me some time to figure that out.The solution I finally went with was to implement a custom deserializer. Works fine. Thanks for your help :) – DMcg Apr 08 '17 at 09:37