Liferay CustomSQL table doesn't exist

597 Views Asked by At

I have a service entity declaration which is as follows:

<entity name="MyContentRepo" local-service="true" remote-service="true" table="contentrepo">
</entity>

I am trying to use Custom SQL to fetch some details:

session = openSession();
String sqlQueryString = CustomSQLUtil.get("query_id");
SQLQuery query = session.createSQLQuery(sqlQueryString);
query.addEntity("MyContentRepo", MyContentRepoImpl.class);
QueryPos qPos = QueryPos.getInstance(query);
qPos.add("someparameter");
list = (List<MyContentRepo>) query.list();

But I get the following error upon execution:

08:02:26,640 ERROR [http-bio-8090-exec-72][JDBCExceptionReporter:82] Table 'mysqldb.mycontentrepo' doesn't exist
com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.exception.SQLGrammarException: could not execute query

The query is taking the name of entity declared not the table="contentrepo". can anyone tell me how to bypass this issue?

1

There are 1 best solutions below

0
On

AddEntity once the SQLQuery has been translated to QueryPos. Change your code as given below:

session = openSession();

String sqlQueryString = CustomSQLUtil.get("query_id"); SQLQuery query = session.createSQLQuery(sqlQueryString);

QueryPos qPos = QueryPos.getInstance(query); qPos.add("someparameter");

query.addEntity("MyContentRepo", MyContentRepoImpl.class); list = (List<MyContentRepo>) query.list();