Я использую Liferay и разрабатываю свой собственный портлет, теперь я хочу использовать собственный запрос для извлечения некоторых данных из нескольких таблиц с объединениями и т. д.
Я гуглил информацию о своей проблеме, но не могу найти простой способ понять пошаговую процедуру.
Поэтому, если кто-нибудь может направить меня или дать мне какое-либо руководство по созданию пользовательского SQL-запроса для моего пользовательского портлета.
после этого 4-го шага я создал свою службу в eclipse и успешно ее показал. В пакете службы/постоянства создано два файла с именами AdvertiseFinder.java
и AdvertiseFinderUtil.java
, но когда я пытаюсь получить доступ к методу getAd_DisplayforReports
с помощью advertiseFinderUtil.getAd_DisplayforReports("Any arguement with string")
, это дает мне ошибку, что нет такого метода в AdvertiseFinderUtil
Я создал службу после обновления моего метода AdvertiseFinderImpl, но он не работает.
это мой класс AdvertiseFinderImpl
package emenu.advertise.database.service.persistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import emenu.advertise.database.model.ad_display;
import emenu.advertise.database.model.advertise;
import emenu.advertise.database.model.impl.ad_displayImpl;
import java.util.List;
import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.util.dao.orm.CustomSQLUtil;
public class AdvertiseFinderImpl extends BasePersistenceImpl<ad_display> implements advertiseFinder{
public void getall() {
}
// the name of the query
public static String GET_ADVERTISE = AdvertiseFinderImpl.class.getName()
+ ".getAdvertise";
// the method which will be called from the ServiceImpl class
public List<ad_display> getAd_DisplayforReports(String pattern) throws SystemException {
Session session = null;
try {
// open a new hibernate session
session = openSession();
// pull out our query from book.xml, created earlier
String sql = CustomSQLUtil.get(GET_ADVERTISE);
// create a SQLQuery object
SQLQuery q = session.createSQLQuery(sql);
// replace the "Book" in the query string with the fully qualified java class
// this has to be the hibernate table name
q.addEntity("a_ad_display", ad_displayImpl.class);
// Get query position instance
QueryPos qPos = QueryPos.getInstance(q);
// fill in the "?" value of the custom query
// this is same like forming a prepared statement
qPos.add(pattern);
// execute the query and return a list from the db
return (List<ad_display>)q.list();
/*
// use this block if you want to return the no. of rows (count)
int rows = 0;
Iterator<Long> itr = q.list().iterator();
if (itr.hasNext()) { Long count = itr.next();
if (count != null) { rows = count.intValue(); } }
return rows;
*/
} catch (Exception e) {
throw new SystemException(e);
} finally {
closeSession(session);
}
}
}
мой default-ext.xml следующий
<?xml version="1.0"?>
<custom-sql>
<sql file="custom-sql/emenu.xml" />
</custom-sql>
мой emenu.xml здесь
<custom-sql>
<sql id="emenu.advertise.database.service.persistence.AdvertiseFinderImpl.getAd_DisplayforReports">
<![CDATA[
SELECT
*
FROM
a_ad_display
]]>
</sql>
</custom-sql>