Reputation: 143
I have 2 POJOs.
A profile class with : id, name, messages (A List of messages) mapped with a One to Many relationship.
A message class with : id, message, profile (Mapped with ManyToOne)
Profile class
@XmlRootElement
@Entity
@Table(name="Profile")
public class Profile {
@Id
@GeneratedValue
@Column(name="profile_id")
private int id;
private String name;
@OneToMany(mappedBy="profile")
private List<Message> messages = new ArrayList<Message>();
Message class
@XmlRootElement
@Entity
@Table(name="Message")
public class Message {
@Id
@GeneratedValue
private int id;
private String message;
@ManyToOne
@JoinColumn(name="profile_id")
private Profile profile;
Post method
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Profile test(){
Profile profile = new Profile(1,"John");
Message message = new Message("Hello World!");
Session session = sessionFactory.openSession();
session.beginTransaction();
profile.getMessages().add(message);
session.save(profile);
message.setProfile(profile);
session.save(message);
session.getTransaction().commit();
session.close();
return profile;
}
When I make the request through Postman in Chrome, I get a "500 Internal Server Error".
The weird part is that it does the insertion in my database. It does it correctly (it maps the message to the profile using the foreign key) but it seems like it can't return the response as a Json.
If I dont save the message ( using session.save(message), it does return me the correct response, but doesn't map it anymore.
How can I get a Json response using One To Many relationship?
Upvotes: 1
Views: 2225
Reputation: 1
I was scratching my head for a full one day on this issue. Same behavior, I was also not able to see any logs on the console. Even @jsonIgnoreproperties was not helping me.
So I went ahead and before returning the response, I iterated through my response object and have set the null value to parent parameter in child object.
List<Message> messages=Profile.getMessage();
for (Message msg:messages){
msg.setProfile(null);
}
This worked for me as I do not need the parent object in my child object.
Upvotes: 0
Reputation: 11
I've found that these 500 Server errors are often caused by circuits in the JSON marshalling process.
For example, Jersey begins to marshall your Profile object into JSON. It does the id, name, and then it begins marshalling the messages property.
It takes the first Message object out of the array and begins marshalling it. The first Message object has an id, name, and a reference back to the Profile object. At this point it follows the Profile object and begins marshalling it all over again.
You can see that you have entered into an unterminated recursive process. The thing that finally stops the recursive marshalling is the 500 Server error you experience when the stack exhausts itself.
To stop this you can either do custom marshalling of your objects or you could simply put a @JsonIgnore property on the profile property in your Message class.
If you find that you need more control then look up look up com.fasterxml.jackson.databind.JsonSerializer.
Upvotes: 1