RE: CustomSQL - Column not found in ResultSet

Pavle Milić, modified 6 Years ago. New Member Posts: 8 Join Date: 12/9/18 Recent Posts
I'm using CustomSQL to run the following query in Liferay v7.1:

<sql id="MenuItem.findItemsByPortalAndOrientation">
   
      SELECT mi.*
      FROM MenuItem mi JOIN Menu m  
      ON m.id = mi.menu_id
      WHERE m.portal_id = ? AND m.orientation = ? AND m.active = true
      ORDER BY mi.createddate DESC
   
</sql>

I have mapped entity aliases to their implementation classes like this:


    @SuppressWarnings("unchecked")
	public List<menuitem> findByPortalAndOrientation(Long portalId, String orientation, int begin, int end) {
		Session session = null;
		try {
			session = openSession();
			String sql = _customSQL.get(getClass(), FIND_ITEMS_BY_PORTAL_AND_ORIENTATION);
			SQLQuery q = session.createSQLQuery(sql);
			q.setCacheable(false);
&nbsp;                        // alias mapping
			q.addEntity("Menu", MenuImpl.class);
			q.addEntity("MenuItem", MenuItemImpl.class);
			LoggerFactory.getLogger(getClass()).info("Query.." + q);
			QueryPos qPos = QueryPos.getInstance(q);
			qPos.add(portalId);
			qPos.add(orientation);
			LoggerFactory.getLogger(this.getClass()).info("" + QueryUtil.list(q, getDialect(), begin, end));
			return (List<menuitem>) QueryUtil.list(q, getDialect(), begin, end);
		} catch (Exception e) {
			try {
				throw new SystemException(e);
			} catch (SystemException se) {
				se.printStackTrace();
			}
		} finally {
			closeSession(session);
		}
		return null;
	}
</menuitem></menuitem>

This is what I get in the log:

2019-02-05 17:36:12.060 INFO  [default task-26][MenuItemFinderImpl:45] Query..{names=null, _sqlQuery=SQLQueryImpl(  SELECT mi.* FROM MenuItem mi JOIN Menu m ON m.id = mi.menu_id WHERE m.portal_id = ? AND m.orientation = ? AND m.active = true ORDER BY mi.createddate DESC   ), _strictName=true}

2019-02-05 17:36:12.082 ERROR [default task-26][JDBCExceptionReporter:234] The column name orientation was not found in this ResultSet.

When I compare the parameter to any column other than "orientation" from the entity Menu it works. Query also succeeds when I use change select clause:

<sql id="MenuItem.findItemsByCourtAndOrientation">
    
        SELECT * // select all instead of mi.*
        FROM MenuItem mi JOIN Menu m  
        ON m.id = mi.menu_id
        WHERE m.portal_id = ? AND m.orientation = ? AND m.active = true
        ORDER BY mi.createddate DESC
    
</sql>
...but in that case the resultset doesn't map to objects correctly as I get the list of weird objects. I tried many other variations of the query but nothing would do.

This is the mapping of this column in service.xml:
(Btw I ran the service builder many times after this)

<entity name="Menu" local-service="true" remote-service="false" table="menu" data-source="extDataSource" cache-enabled="false">
    <column name="id" db-name="id" primary="true" type="long" />
    <column name="orientation" db-name="orientation" type="String" />
    ...
    ...
</entity>

What could be the cause of this error?
thumbnail
Amos Fong, modified 6 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts
If you're only retrieving menuItem objects, then I don't think you need to add the Menu entity to the query. Try removing this line:
q.addEntity("Menu", MenuImpl.class);

Then you should be able to use this again:
SELECT mi.*
Pavle Milić, modified 6 Years ago. New Member Posts: 8 Join Date: 12/9/18 Recent Posts
Thank you Amos, adding the entity class only for returning entity works!