Service Builder: custom query using inner join across the same table

65 Views Asked by At

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?

(1) Lets suppose we have this custom sql:

select e1.*, e2.* from entity as e1 inner join entity as e2 on e1.id=e2.parentid;

(2) Lets suppose we have this custom finder method too:

public List findMyEntityList() throws SystemException {
        Session session = null;

        try {
            session = openSession();
            String sql = ...
            SQLQuery queryObject = session.createSQLQuery(sql);
            queryObject.setCacheable(false);
            queryObject.addEntity("entity1", EntityImpl.class);
            queryObject.addEntity("entity2", 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;
    }

(3) And lets suppose this code is the one in charge of reading the values recovered by the .findMyEntityList():

for (Object object : this.myEntityList) {
    Object[] objectArray = (Object[]) object;
    e1 = (Entity) objectArray[0];
    e2 = (Entity) objectArray[1];
    ...
}

When I check the size of the myEntityList is 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).

Any comment or clarification would be appreciate.

1

There are 1 best solutions below

0
sandro On

As stated here https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#d0e13763 you should wrap your sql entities in curly braces, that is, your SQL should start with

select {e1.*}, {e2.*} from entity...

Doing this way, the following addEntity() calls should work as expected.