sql - How to fetch liferay entity through custom-finder in custom plugin portlet? -


how can fetch liferay entities through custom-finder using custom sql?

  1. following sql query written in default.xml (i have trimmed down query bare minimum logic remains simple. since included few functions , joins couldn't use dynamicquery api ):

    select     grp.*     group_     site = 1     , active_ = 1     , type_ <> 3 
  2. relevant code in mycustomgroupfinderimpl.java:

    session session = null;  try {     session = opensession();      // fetches query string default.xml     string sql = customsqlutil.get(find_only_active_sites);      sqlquery sqlquery = session.createsqlquery(sql);      sqlquery.addentity("group_", groupimpl.class);     // sqlquery.addentity("group_", portalclassloaderutil.getclassloader().loadclass("com.liferay.portal.model.impl.groupimpl"));      return (list<group>) queryutil.list(sqlquery, getdialect(), 0, queryutil.all_pos); } catch (exception e) {     throw new systemexception(e); } {     closesession(session); } 

this above code won't work groupimpl class present in portal-impl.jar , jar cannot used in custom portlet.

i tried using sqlquery.addentity("group_", portalclassloaderutil.getclassloader().loadclass("com.liferay.portal.model.impl.groupimpl"))
above code throws exception:

com.liferay.portal.kernel.exception.systemexception:     com.liferay.portal.kernel.dao.orm.ormexception:         org.hibernate.mappingexception:             unknown entity: com.liferay.portal.model.impl.groupimpl 

but same code works our custom-entity, if write sqlquery.addentity("mycustomgroup", mycustomgroupimpl.class);.

thanks

i found out liferay forum thread instead of session = opensession(); need fetch session liferaysessionfactory follows make work:

// fetch liferay's session factory sessionfactory sessionfactory = (sessionfactory) portalbeanlocatorutil.locate("liferaysessionfactory");  session session = null;  try {     // open session using liferay's session factory     session = sessionfactory.opensession();      // fetches query string default.xml     string sql = customsqlutil.get(find_only_active_sites);      sqlquery sqlquery = session.createsqlquery(sql);      // use portal class loader, since portal entity     sqlquery.addentity("group_", portalclassloaderutil.getclassloader().loadclass("com.liferay.portal.model.impl.groupimpl"));      return (list<group>) queryutil.list(sqlquery, getdialect(), 0, queryutil.all_pos); } catch (exception e) {     throw new systemexception(e); } {     sessionfactory.closesession(session); // edited per comment on answer     // closesession(session); } 

hope helps on stackoverflow, found nice tutorial regarding custom-sql uses same approach.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -