<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Custom query using inner join across the same table in Service Builder.</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=121494277" />
  <subtitle>Custom query using inner join across the same table in Service Builder.</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=121494277</id>
  <updated>2026-04-06T03:45:00Z</updated>
  <dc:date>2026-04-06T03:45:00Z</dc:date>
  <entry>
    <title>Custom query using inner join across the same table in Service Builder.</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121494276" />
    <author>
      <name>txapeldot .</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121494276</id>
    <updated>2022-08-12T07:46:59Z</updated>
    <published>2022-08-12T07:46:59Z</published>
    <summary type="html">&lt;p&gt;Does anybody know if Liferay Service Builder runs fine when has to
  deal with a custom-sql consisting of an inner join clause across the
  same table?&lt;/p&gt;
&lt;p&gt;(1) Lets suppose we have this custom sql:&lt;/p&gt;
&lt;pre&gt;
&lt;code class="language-html"&gt;select e1.*, e2.* from entity as e1 inner join entity as e2 on e1.id=e2.parentid;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(2) Lets suppose we have this custom finder method too:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;public List findMyEntityList() throws SystemException {
        Session session = null;

        try {
            session = openSession();
            String sql = ...
            SQLQuery queryObject = session.createSQLQuery(sql);
            queryObject.setCacheable(false);
            queryObject.addEntity(&amp;quot;entity1&amp;quot;, EntityImpl.class);
            queryObject.addEntity(&amp;quot;entity2&amp;quot;, EntityImpl.class);
            QueryPos qPos = QueryPos.getInstance(queryObject);

            return (List) QueryUtil.list(queryObject, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);          
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            closeSession(session);
        }

        return null;
    }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(3) And lets suppose this code is the one in charge of reading the
  values recovered by the &lt;code&gt;.findMyEntityList()&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;for (Object object : this.myEntityList) {
    Object[] objectArray = (Object[]) object;
    e1 = (Entity) objectArray[0];
    e2 = (Entity) objectArray[1];
    ...
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When I check the size of the &lt;code&gt;myEntityList&lt;/code&gt; t's the one
  expected. However, when I print the values of both e1 and e2 model
  entities, they all are equal among them; it seems that e1 equals e2,
  that is, it seems that Service Builder is not able of distingishing
  that it has to store two diferent 'Entity' model classes (the one on
  the left side on the inner join clause, and the one on the right side).&lt;/p&gt;
&lt;p&gt;Any comment or clarification would be appreciate.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;</summary>
    <dc:creator>txapeldot .</dc:creator>
    <dc:date>2022-08-12T07:46:59Z</dc:date>
  </entry>
</feed>
