• hibernate4--OpenSessionInViewFilter


    场景:hibernate3更换hibernate4后,查询对象User时抛如下异常

    java.lang.ClassCastException: cn.xx.po.developer.GameLite_$$_javassist_2 cannot be cast to javassist.util.proxy.Proxy
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:148)
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:73)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:758)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4419)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:334)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1017)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:944)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:714)
    at org.hibernate.type.EntityType.resolve(EntityType.java:502)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:137)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1112)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:969)
    at org.hibernate.loader.Loader.doQuery(Loader.java:917)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)
    at org.hibernate.loader.Loader.doList(Loader.java:2550)
    at org.hibernate.loader.Loader.doList(Loader.java:2536)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366)
    at org.hibernate.loader.Loader.list(Loader.java:2361)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:198)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1194)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    at cn.xx.util.dbUtil.GenericDaoImpl.findTop(GenericDaoImpl.java:84)
    at cn.xx.service.player.impl.UserInfoServiceImpl.findUsers(UserInfoServiceImpl.java:93)

     解决方法:

    1.方法一

    打开User.hbm.xml文件

    <many-to-one column="GameID" name="gameID" class="cn.xx.po.developer.GameLite"/>

    在对应的many-to-one中添加

    lazy="false"

    2. 方法二

    在web.xml文件添加(注:配置在Struts2的核心Filter前面)

    <filter >   
        <filter-name > HibernateOpenSession</filter-name >    
        <filter-class > org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class >   
    </filter >   
    <filter-mapping >   
        <filter-name > HibernateOpenSession</filter-name >   
        <url-pattern > /*</url-pattern >   
    </filter-mapping >

    注:OpenSessionInViewFilter类中有个属性叫 sessionFactoryBeanName默认值为sessionFactory,如果Spring中配置的SessionFactory不叫这个名字时,需要指定SessionFactory的名字,否则会引起org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined异常。

    <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>mySessionFactory</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

  • 相关阅读:
    Docker安装MySQL&Redis
    使用VirtualBox+Vagrant快速搭建Linux虚拟机环境
    Java集合工具类的一些坑,Arrays.asList()、Collection.toArray()...
    1.docker常用命令
    4. 带有延迟时间的Queue(DelayQueue)
    3. 基于优先级的Queue(PriorityBlockingQueue)
    2. 常见的Queue
    1. 模拟Queue
    14. 线程调度
    13. 线程池
  • 原文地址:https://www.cnblogs.com/tongxinyuan/p/4432979.html
Copyright © 2020-2023  润新知