Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA - Paginated query with ManyToMany collection returns wrong number of entities
    primarykey
    data
    text
    <p>I have these two entities (here are simplified versions) :</p> <p>Topic : </p> <pre><code>@Entity public class Topic implements Serializable { @Id @GeneratedValue( strategy = GenerationType.IDENTITY ) private Long id; @ManyToMany @JoinTable( name = "topic_member", joinColumns = @JoinColumn( name = "topic" ), inverseJoinColumns = @JoinColumn( name = "member" ) ) @JoinFetch private List&lt;Member&gt; participants = new ArrayList&lt;Member&gt;(); ... </code></pre> <p>Member :</p> <pre><code>@Entity public class Member implements Serializable { @Id @GeneratedValue( strategy = GenerationType.IDENTITY ) private Long id; ... </code></pre> <p>Those are related through a join table <code>topic_member(member, topic)</code>, via a unidirectionnal @ManyToMany relationship.</p> <p>Everything works just fine when I insert stuff, no problem here.</p> <p>But when I want to select stuff, it becomes more tricky. Let's say I want to select all the topics related to a particular member. I wrote the following JPQL query:</p> <pre><code>SELECT t FROM Topic t WHERE :member MEMBER OF t.participants ORDER BY t.id DESC </code></pre> <p>And then I wrote this:</p> <pre><code>TypedQuery&lt;Topic&gt; query = em.createQuery( MY_JPQL_REQUEST, Topic.class ); query.setParameter( PARAM_MEMBER, member ); return query.getResultList(); </code></pre> <p>And it works just fine.</p> <p>But something is very weirdly wrong : it seems I can't use pagination on this query! For example, when I try this:</p> <pre><code>TypedQuery&lt;Topic&gt; query = em.createQuery( MY_JPQL_REQUEST, Topic.class ); query.setParameter( PARAM_MEMBER, member ); query.setFirstResult( 0 ); query.setMaxResults( 5 ); return query.getResultList(); </code></pre> <p>It returns me a wrong number of entities. For example, when the previous one returns 5, it returns only 3...</p> <p>What am I missing here? I can't understand what could go wrong.</p> <p>I've read <a href="http://www.eclipse.org/forums/index.php/t/321611/" rel="nofollow">this page</a> and I think there may be something here, but I can't figure out what.</p> <p><strong>[Edit] Here is what I have in the join table in the DB, if it helps:</strong></p> <pre> +-------------+--------------+ | member | topic | +-------------+--------------+ | 1 | 1 | | 1 | 2 | | 1 | 3 | | 1 | 4 | | 1 | 5 | | 2 | 1 | | 2 | 2 | | 2 | 3 | | 2 | 4 | | 2 | 5 | +-------------+--------------+ </pre> <p><strong>[Solution] Thanks to Chris, I got it working. I had to replace <code>@JoinFetch</code> by <code>@BatchFetch( BatchFetchType.JOIN )</code> on the <code>@ManyToMany</code> field, and to query <code>SELECT DISTINCT(t) FROM ...</code> instead of <code>SELECT t FROM ...</code>.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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