Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: CustomSQL - Column not found in ResultSet
I'm using CustomSQL to run the following query in Liferay v7.1:
I have mapped entity aliases to their implementation classes like this:
This is what I get in the log:
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:
This is the mapping of this column in service.xml:
(Btw I ran the service builder many times after this)
What could be the cause of this error?
<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);
// 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?
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:
Then you should be able to use this again:
q.addEntity("Menu", MenuImpl.class);
Then you should be able to use this again:
SELECT mi.*
Thank you Amos, adding the entity class only for returning entity works!
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™