0

我正在使用 liferay,这里是我的类和方法:

EmployeeLocalServiceImpl.java

public class EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never reference this interface directly. Always use {@link com.mallyas.corpserver.emco.mis.service.EmployeeLocalServiceUtil} to access the employee local service.
     */

    public List getEmployee() throws PortalException, SystemException, RemoteException {

        return EmployeeFinderUtil.getEmployee();
    }
 ...
}

EmployeeFinderImpl.java

public class EmployeeFinderImpl extends BasePersistenceImpl<Employee> implements EmployeeFinder {

    // the name of the query
    public static String GET_EMPLOYEE = "getemployee";

    // the method which will be called from the ServiceImpl class

    public List<Employee> getEmployee() throws SystemException {

        Session session = null;
        try {
            // open a new hibernate session
            session = openSession();


            String fetchEmpsqlquery = CustomSQLUtil.get(GET_EMPLOYEE);

            // create a SQLQuery object
            SQLQuery empqueryObj = session.createSQLQuery(fetchEmpsqlquery);

            // replace the "Book" in the query string with the fully qualified java class
            // this has to be the hibernate table name
            empqueryObj.addEntity("Employee", EmployeeImpl.class);


            // Get query position instance
            QueryPos qPos = QueryPos.getInstance(empqueryObj);

            // 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<Employee>)empqueryObj.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);
        }
    }

    ...

}

EmployeeFinder.java

@Transactional
public interface EmployeeFinder {

    public java.util.List<com.mallyas.corpserver.emco.mis.model.Employee> getEmployee()
        throws com.liferay.portal.kernel.exception.SystemException;
}

EmployeeFinderUtil.java

@Transactional
public class EmployeeFinderUtil {

    public static java.util.List<com.mallyas.corpserver.emco.mis.model.Employee> getEmployee()
        throws com.liferay.portal.kernel.exception.SystemException {
        return getFinder().getEmployee();
    }

    public static EmployeeFinder getFinder() {
        if (_finder == null) {
            _finder = (EmployeeFinder)PortletBeanLocatorUtil.locate(com.mallyas.corpserver.emco.mis.service.ClpSerializer.getServletContextName(),
                    EmployeeFinder.class.getName());

            ReferenceRegistry.registerReference(EmployeeFinderUtil.class,
                "_finder");
        }

        return _finder;
    }

    public void setFinder(EmployeeFinder finder) {
        _finder = finder;

        ReferenceRegistry.registerReference(EmployeeFinderUtil.class, "_finder");
    }

    private static EmployeeFinder _finder;

运行此应用程序时出现以下错误:

ERROR [http-bio-8080-exec-9][render_portlet_jsp:154] com.liferay.portal.kernel.exception.SystemException: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at com.mallyas.corpserver.emco.mis.service.persistence.EmployeeFinderImpl.getEmployee(EmployeeFinderImpl.java:69)
    at com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice.invoke(ShardPersistenceAdvice.java:52)
    at com.mallyas.corpserver.emco.mis.service.persistence.EmployeeFinderUtil.getEmployee(EmployeeFinderUtil.java:29)
    at com.mallyas.corpserver.emco.mis.service.impl.EmployeeLocalServiceImpl.getEmployee(EmployeeLocalServiceImpl.java:61)
    at org.apache.jsp.admin.displayData.dbcheck_jsp._jspService(dbcheck_jsp.java:172)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)

什么可能是错的?我在上述每个类中都包含了@Transactional,我还尝试从所有类中删除@Transactional,但我仍然得到同样的错误..

4

4 回答 4

0

我认为您在从 FinderImpl 或 Finder 类调用方法时可能会遇到这些异常。我还猜测您正在使用 Finder 类来执行自定义查询。将该方法命名为“getEmployeeByCustom”

您不需要编辑 Finder.java 或 FinderUtil.java 类,只需 FinderImpl.java 一个。

确保在 EmployeeLocalServiceImpl 中使用包装函数

    public List<Employee> getEmployeeByCustom(.....) 
        throws SystemException {

    return EmployeeFinderUtil.getEmployeeByCustom(....);

}

然后,通过 EmployeeLocalServiceUtil.getEmployeeByCustom(...) 调用“getEmployeeByCustom”

您应该撤消与“事务性”相关的编辑,您并不是要弄乱它们。另外,请务必查看本教程

于 2013-03-19T09:07:24.023 回答
0

当您调用 Liferay *Util 类时,事务永远不会传播。

如果您在 *ServiceImpl 中使用 *ServiceUtil,也会出现同样的问题。

在您的情况下,我认为您可以使用必须注入到您的 EmployeeLocalServiceImpl 中的 EmployeePersistence。

替换此代码:return EmployeeFinderUtil.getEmployee();

通过这个return this.employeePersistence.getEmployee();

于 2014-01-27T12:33:53.837 回答
0

尝试在休眠配置中添加以下属性:

<prop key="hibernate.current_session_context_class">thread</prop>

这基本上将在每个线程的基础上绑定会话。

于 2013-03-18T06:05:08.203 回答
0

使用 Liferay 6.1.20 和我使用 maven 和 Liferay 插件版本 6.2.10.9 的构建。

确保您的 EmployeeFinderImpl 类在您的 service.persistence 包/文件夹中。

当服务构建器运行时,它会为您的 EmployeeFinderImpl 生成接口。此外,它添加代码以将其实例化到您的 EmployeeLocalServiceImpl 中。

EmployeeLocalServiceImpl 应该已经在其中实例化了您的 EmployeeFinderImpl 类,您可以将代码更改为以下代码,它将起作用。

public class EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never reference this interface directly. Always use {@link com.mallyas.corpserver.emco.mis.service.EmployeeLocalServiceUtil} to access the employee local service.
     */

    public List getEmployee() throws PortalException, SystemException, RemoteException {

        return employeeFinder.getEmployee();
    }
 ...
}
于 2015-10-02T19:36:30.333 回答