Hibernate Many to Many and JSON serialization
<p>I'm using <code>Hybernate</code> to persist some beans that have a Many-To-Many relationship. Here are the two beans:</p>

<p><strong>Email.java</strong></p>

<pre><code>@Entity
@NamedQuery(name = "Email.findAll", query = "SELECT e FROM Email e")
public class Email implements Serializable {
    private static final long serialVersionUID = 1L;

    //... other fields ...

    // bi-directional many-to-many association to Pratica
    @ManyToMany()
    @JoinTable(name = "email_pratica", joinColumns = {
            @JoinColumn(name = "fk_email_id")
    }, inverseJoinColumns = {
            @JoinColumn(name = "fk_pratica_id")
    })
    private List&lt;Pratica&gt; pratiche;

    public List&lt;Pratica&gt; getPratiche() {
        return this.pratiche;
    }

    public void setPratiche(List&lt;Pratica&gt; pratiche) {
        this.pratiche = pratiche;
    }

}
</code></pre>

<p><strong>Pratica.java</strong></p>

<pre><code>@Entity
@NamedQuery(name = "Pratica.findAll", query = "SELECT p FROM Pratica p")
public class Pratica extends AtstTable implements Serializable {
    private static final long serialVersionUID = 1L;

    //... other fields ...

    // bi-directional many-to-many association to Email
    @ManyToMany(mappedBy = "pratiche", fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    private List&lt;Email&gt; emails;

    public List&lt;Email&gt; getEmails() {
        return this.emails;
    }

    public void setEmails(List&lt;Email&gt; emails) {
        this.emails = emails;
    }

}
</code></pre>

<p>This works correctly, however the problem is that I then need to serialize/deserialize these beans using <code>JSON</code>. Doing this causes an infinite recursion, since each object <code>Email</code> tries to serialize all of its objects <code>Pratica</code>, which in turn try to serialize all their <code>Email</code>s and so on, until a <code>StackOverflow</code> exception occours.</p>

<p>I've searched online and here on SO, and I've found several solutions to this problem, however none of them work or are applicable, due to the configuration of my project. In particular, here's what I've tried:</p>

<p><strong>Solution 1: using <code>@JsonManagedReference</code> / <code>@JsonBackReference</code></strong></p>

<p>This seems to be the most suggested solution. However, this only works (as far as I can tell) for One-To-Many or Many-To-One relationships, since the <code>@JsonBackReference</code> annotation does not support collections (Lists, Arrays, Sets, etc.). Or at least, this is what the documentation says and when trying I got an exception that seems to confirm this. I've found a couple of examples online that use this annotation on a List, but for me it didn't work. If anyone has any clue on this, please elaborate.</p>

<p><strong>Solution 2: do not persist one side of the relationship using <code>@JsonIgnore</code></strong></p>

<p>Adding <code>@JsonIgnore</code> to one of the fields, like this:</p>

<pre><code>@ManyToMany()
@JoinTable(name = "email_pratica", joinColumns = {
        @JoinColumn(name = "fk_email_id")
}, inverseJoinColumns = {
        @JoinColumn(name = "fk_pratica_id")
})
@JsonIgnore
private List&lt;Pratica&gt; pratiche;
</code></pre>

<p>breaks the infinite loop, and thus works. It would also suit me, since I don't really need the list of <code>Pratica</code> on the <code>Email</code> objects, I never use it.
However, this introduces another big problem: whenever I persist the object back to the database the list of Pratica will be empty (since it was lost during <code>JSON</code> serialization), and thus Hibernate will delete the association record from my Join Table.</p>

<p><strong>Solution 3: using <code>@JsonIdentityInfo</code></strong><br>
This solution needs <code>Jackson 2.x</code>, but unfortunately, due to restrictions on the server that are beyond my control, I can only use the version of Jackson provided by <code>RESTeasy</code> (<code>resteasy-jackson-provider 2.3.6</code>), which provides a subset of <code>Jackson 1.x</code></p>

<p><strong>Solution 4: using a custom serializer/deserializer</strong><br>
See solution 3. Even tho the <code>@JsonSerialize</code>/<code>@JsonDeserialize</code> annotations are included in <code>Jackson 1.1</code>, it seems they're not included in the version provided by RESTeasy, or at least the compiler can't find the references, so I assume this is the case.</p>

<p>So, after all this... is there a clean solution to this problem with the restrictions I have? Can it be done in a non-hacky way? And if it can't, what is the "less messy" non-hacky way?</p>