我有一个自定义sql,我为它写了一个finder类:
public class PopulationFinder
{
public static String TABLE_NAME = "public.population";
public static String FINDBYNUM = "ir.nsdp.srp.ruraldevelopment.portlet.ac.persistence.PopulationFinderImpl.findByNum";
public List<Population> findByNum(OrderByComparator obc, String lowerRangeEqual,
int lowerRange, String upperRangeEqual, int upperRange, int start, int end) throws SystemException {
Session session = null;
try {
session = openSession();
String sql = CustomSQLUtil.get(FINDBYRUSTA);
sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(null));
sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(null));
sql = CustomSQLUtil.replaceOrderBy(sql, obc);
SQLQuery q = session.createSQLQuery(sql);
q.setCacheable(false);
q.addEntity(TABLE_NAME, PopulationImpl.class);
QueryPos qPos = QueryPos.getInstance(q);
qPos.add(lowerRangeEqual);
qPos.add(lowerRange);
qPos.add(upperRangeEqual);
qPos.add(upperRange);
if (!Validator.isNull(obc))
qPos.add(obc);
return (List<Population>) QueryUtil.list(q, getDialect(), start, end);
} catch (Exception e) {
throw new SystemException(e);
} finally {
closeSession(session);
}
}
}
我的问题是我无法访问openSession方法,因为我没有扩展BasePersistenceImpl。 如果我扩展它,因为我想让我的findByNum方法是静态的,所以我可以从任何地方调用它,它使openSession错误,因为我做了一个非静态引用。
我想在此方法中手动创建会话。这可能吗?