Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <code>@JsonIdentityInfo</code> in the two classes <code>User</code> and <code>Event</code> this way:</p> <pre class="lang-java prettyprint-override"><code>import java.util.List; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class, property="@UUID") public class User { private int id; private List&lt;Event&gt; events; // Getters and setters } </code></pre> <p>... and</p> <pre class="lang-java prettyprint-override"><code>import java.util.List; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class, property="@UUID") public class Event { private int id; private List&lt;User&gt; users; // Getters and setters } </code></pre> <p>You can use any of the <code><a href="http://fasterxml.github.io/jackson-annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/ObjectIdGenerator.html" rel="noreferrer">ObjectIdGenerator</a></code>s as appropriate. Now, serialization and deserialization of the objects that correspond to the many to many mapping will succeed:</p> <pre class="lang-java prettyprint-override"><code>public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); Event event1 = new Event(); event1.setId(1); Event event2 = new Event(); event2.setId(2); User user = new User(); user.setId(10); event1.setUsers(Arrays.asList(user)); event2.setUsers(Arrays.asList(user)); user.setEvents(Arrays.asList(event1, event2)); String json = objectMapper.writeValueAsString(user); System.out.println(json); User deserializedUser = objectMapper.readValue(json, User.class); System.out.println(deserializedUser); } </code></pre> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload