Liferay sessionFactory.getCurrentSession()报错问题

刚开始研究liferay不久,不过对liferay的功能和用的技术真多比较佩服.不过对于开发人员来说要深入了解那么的技术的不是那么容易的事情.

liferay的资料相对较少,不过去官方的论坛和wiki里面找一些东西.

 

最近在使用liferay的plugin portlet开发方式.

最简单的功能就是直接从数据库查询数据,插入数据等.

插入数据的话用liferay的build service工具就能自动提供接口不用那么麻烦.

 

不过查询的时候从BasePersistenceImpl类的openSession()的时候报了下面的错误.

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.test.finder.service.persistence.TestEntryFinderImpl.findByNoAssets(TestEntryFinderImpl.java:256)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
    at com.liferay.portal.dao.shard.ShardAdvice.invokePersistence(ShardAdvice.java:205)
    at sun.reflect.GeneratedMethodAccessor524.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy348.findByNoAssets(Unknown Source)

 

 

郁闷了好几天.找了好些liferay的东西看也不知道解决办法.

最近跟踪代码,发现里面是sessionFactory.getCurrentSession()这个方法报错.

 

当时想你报错那我就不用你好了,用Session openNewSession(Connection connection)这个方法好了.

openNewSession这个方法倒是能够正常访问了,但这个是每次都打开一个新连接,然后关闭.

还是感觉用springde东西比较好.

没办法回到sessionFactory.getCurrentSession()为什么出错上来,在网上找了很久,才发现了解决办法.

service builder执行后的默认定义.

   <bean id="liferayHibernateSessionFactory"
        class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
        <property name="dataSource" ref="liferayDataSource" />
    </bean>
   
   
修改后的定义:
    <bean id="liferayHibernateSessionFactory"
        class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
        <property name="dataSource" ref="liferayDataSource" />
         <property name="hibernateProperties">
            <props>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>

    </bean>

 

 

需要把这个上下文定义为线程.主要还是对hibernate和spring不熟悉,花费了不少时间.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值