Hibernate异常_ $$ _ javassist_0无法强制转换为javassist.util.proxy.Proxy

时间:2014-03-18 13:55:38

标签: java hibernate java-ee orm proxy

你好我正在使用developnig java web应用程序,当我尝试使用hibernate获取数据时,我得到了下一个异常

java.lang.ClassCastException: com.digitalticket.model.UserType_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy

这里是stacktrace

java.lang.ClassCastException: com.digitalticket.model.UserType_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:147)
    at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:75)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:771)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4613)
    at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:349)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:270)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:989)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716)
    at org.hibernate.type.EntityType.resolve(EntityType.java:502)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    at com.digitalticket.model.DAO.fetchAll(DAO.java:204)
    at com.digitalticket.controller.IndexController.handleRequestInternal(IndexController.java:22)
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:154)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3363)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2146)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2124)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
    at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:550)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:254)

这是我的代码

public T fetchAll(tClass<T> className) {

    Session s = sessionFactory.getCurrentSession();
    s.beginTransaction();

    try {

        List<T> results = (List<T>) sessionFactory.getCurrentSession()
                .createCriteria(className)
                .list();

        s.getTransaction().commit();

        return results();

    } catch (NullPointerException ex) {
        return null;
    }
    catch (RuntimeException re) {
        s.getTransaction().rollback();
        throw re;
    } finally {

    }
}

这是我的对象

public class User implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2383716625869790753L;
    private long userId;
    private UserType userType;
    private String email;
    private String password;
    private String name;
    private String surname;
    private String middlename;
    private Set<Ticket> tickets = new HashSet<Ticket>(0);
    private Set<Organization> organizations = new HashSet<Organization>(0);

    public User() {
    }

    public User(long userId, String email, String password, String name,
            String surname) {
        this.userId = userId;
        this.email = email;
        this.password = password;
        this.name = name;
        this.surname = surname;
    }

    public User(long userId, UserType userType, String email, String password,
            String name, String surname, String middlename,
            Set<Ticket> tickets, Set<Organization> organizations) {
        this.userId = userId;
        this.userType = userType;
        this.email = email;
        this.password = password;
        this.name = name;
        this.surname = surname;
        this.middlename = middlename;
        this.tickets = tickets;
        this.organizations = organizations;
    }

    public long getUserId() {
        return this.userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public UserType getUserType() {
        return this.userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getMiddlename() {
        return this.middlename;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public Set<Ticket> getTickets() {
        return this.tickets;
    }

    public void setTickets(Set<Ticket> tickets) {
        this.tickets = tickets;
    }

    public Set<Organization> getOrganizations() {
        return this.organizations;
    }

    public void setOrganizations(Set<Organization> organizations) {
        this.organizations = organizations;
    }

}

public class UserType implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -206438165274679246L;
    private long userTypeCode;
    private String userTypeName;
    private Set<User> users = new HashSet<User>(0);

    public UserType() {
    }

    public UserType(long userTypeCode, String userTypeName) {
        this.userTypeCode = userTypeCode;
        this.userTypeName = userTypeName;
    }

    public UserType(long userTypeCode, String userTypeName, Set<User> users) {
        this.userTypeCode = userTypeCode;
        this.userTypeName = userTypeName;
        this.users = users;
    }

    public long getUserTypeCode() {
        return this.userTypeCode;
    }

    public void setUserTypeCode(long userTypeCode) {
        this.userTypeCode = userTypeCode;
    }

    public String getUserTypeName() {
        return this.userTypeName;
    }

    public void setUserTypeName(String userTypeName) {
        this.userTypeName = userTypeName;
    }

    public Set<User> getUsers() {
        return this.users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

}

这是映射

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 16, 2014 10:31:53 AM by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
    <class name="com.digitalticket.model.User" table="&quot;USER&quot;">
        <id name="userId" type="long">
            <column name="USER_ID" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <many-to-one class="com.digitalticket.model.UserType"
            fetch="select" name="userType">
            <column name="USER_TYPE" precision="10" scale="0" />
        </many-to-one>
        <property generated="never" lazy="false" name="email" type="string">
            <column length="20" name="EMAIL" not-null="true" unique="true" />
        </property>
        <property generated="never" lazy="false" name="password"
            type="string">
            <column length="32" name="PASSWORD" not-null="true" />
        </property>
        <property generated="never" lazy="false" name="name" type="string">
            <column length="64" name="NAME" not-null="true" />
        </property>
        <property generated="never" lazy="false" name="surname" type="string">
            <column length="64" name="SURNAME" not-null="true" />
        </property>
        <property generated="never" lazy="false" name="middlename"
            type="string">
            <column length="64" name="MIDDLENAME" />
        </property>
        <set fetch="select" inverse="true" lazy="true" name="tickets"
            sort="unsorted" table="TICKET">
            <key>
                <column name="USER" precision="10" scale="0" />
            </key>
            <one-to-many class="com.digitalticket.model.Ticket" />
        </set>
        <set fetch="select" lazy="true" name="organizations" sort="unsorted"
            table="AUDITOR">
            <key>
                <column name="USER" not-null="true" precision="9" scale="0" />
            </key>
            <many-to-many entity-name="com.digitalticket.model.Organization"
                unique="false">
                <column name="ORGANIZATION" not-null="true" precision="10"
                    scale="0" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 16, 2014 10:31:53 AM by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
    <class name="com.digitalticket.model.UserType" table="USER_TYPE">
        <id name="userTypeCode" type="long">
            <column name="USER_TYPE_CODE" precision="10" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="userTypeName" type="string">
            <column name="USER_TYPE_NAME" length="40" not-null="true" unique="true" />
        </property>
        <set name="users" table="USER" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="USER_TYPE" precision="10" scale="0" />
            </key>
            <one-to-many class="com.digitalticket.model.User" />
        </set>
    </class>
</hibernate-mapping>

我正在使用Oracle Weblogic 12c服务器

14 个答案:

答案 0 :(得分:25)

我使用hibernate 4.3.5.Final并且与javassist有类似的问题,问题是缺少javassist,你可以从Maven repositorty获得最新版本。

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.18.1-GA</version> 
</dependency>

在以后的版本中,它再次修复

在我的案例中,所有这些都有效,

答案 1 :(得分:18)

在我的案例中,问题是classpath中有两个javassist库。一个来自org.hibernate,另一个来自org.apache.struts.xwork。通过删除后者来解决。

答案 2 :(得分:11)

就像anquegi已经评论过的那样。你有一个lib冲突。返回hibernate到4.2.7.Final不会解决你的lib冲突。 运行mvn dependency:tree -Dverbose并查看javassist的不同版本。在我的例子中,我需要从Hibernate依赖项中排除javassist。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${hibernate.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
        </exclusion>
    </exclusions>
</dependency>

这可能有助于未来的其他人。

答案 3 :(得分:10)

看起来Weblogic本身包含比您的应用程序更旧的javaassist。它可以在应用程序描述符,weblogic.xml或weblogic-application.xml中修复。只需添加

<prefer-application-packages>
    <package-name>javassist</package-name>        
</prefer-application-packages>

进入root配置元素。

顺便说一句,您可以使用Weblogic Classloader Analysis Tool检查此类冲突 - 可以在服务器的/ wls-cat上下文路径中访问它。

答案 4 :(得分:10)

虽然只是与您的问题部分相关,但我想在某处发布此信息,以帮助在使用SpringMVC,Hibernate和Apache Tiles时遇到此问题的任何人。

当我将org.apache.tiles tiles-extras列为依赖项时,我得到了这个异常。 Tiles-extras和hibernate-core将不同版本的javassist列为依赖项。通过将以下内容添加到我的pom.xml文件中,我能够修复错误。

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-extras</artifactId>
    <version>3.0.5</version>
    <exclusions>
        <exclusion>
            <artifactId>javassist</artifactId>
            <groupId>jboss</groupId>
        </exclusion>
    </exclusions>
</dependency>

答案 5 :(得分:8)

通过将我的pom.xml中的依赖项更改为旧版本4.3.4.Final来解决 错误在4.3.4中。最终版本更改为4.2.7.Final

不要认为这是最好的解决方案,但我没有找到任何other

答案 6 :(得分:5)

对于hibernate 4.2.x,我在JBoss EAP 7.1.3(EAP 6.0.1)上遇到了类似的问题。因为JBoss模块已经包含了javassist lib。

我通过在WEB-INF目录中使用以下内容创建 jboss-deployment-structure.xml 解决了这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
  <deployment>
    <exclusions>
      <module name="org.javassist"/>
    </exclusions>
    <local-last value="true" />
  </deployment>
</jboss-deployment-structure>

答案 7 :(得分:3)

我一直在使用4.2.8-hibernate的最终版本,并且遇到了这个问题并且之前已经完成了以下的答案/建议。以下是我所做的事件序列 - 为了摆脱4.2.8-Final的问题,在Websphere中使用了“父类最后”类加载器方法。最近,当项目规模扩大时(通过更多与hibernate相关的jar风味),开始得到同样的问题。

最后从hibernate

中排除了“javassist”
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
    <exclusion>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
    </exclusion>
</exclusions>

并添加了

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version> 

即便这样也没有完全解决问题(我仍然不得不使用'父亲最后'类加载器方法与hibernate解决问题。

最后我将hibernate版本从4.2.8-Final降级到4.2.7-Final,问题解决了(我不需要在websphere管理控制台中将我的类装入器首选项更改为父级了)

不完全确定4.2.8 Hibernate中的哪些更改导致此问题。很想知道。

答案 8 :(得分:2)

解决了基于weblogic的spring-hibernate-jpa应用程序的问题。由于Web逻辑服务器和javassist jar的jar冲突,因此发生了类强制转换异常。将javassist-3.18.0-ga.jar或更高版本添加到类路径中,然后将以下代码行添加到weblogic-application.xml文件中,它应该可以解决您的问题。

<wls:prefer-application-packages>
        <wls:package-name>javassist.*</wls:package-name>
  </wls:prefer-application-packages>

答案 9 :(得分:0)

检查结果的返回类型TList<T>

答案 10 :(得分:0)

我遇到了与Spring Data JPA相同的问题。我确认它来自javassit冲突。 解决方案是:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <artifactId>javassist</artifactId>
            <groupId>org.javassist</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.18.2-GA</version>
    <scope>compile</scope>
</dependency>

答案 11 :(得分:0)

使用hibernate + titles时遇到了同样的问题。通过从tiles-extras中排除javassist来解决:

<dependency>
  <groupId>org.apache.tiles</groupId>
  <artifactId>tiles-extras</artifactId>
  <version>3.0.5</version>
  <exclusions>
    <exclusion>
      <groupId>jboss</groupId>
      <artifactId>javassist</artifactId>
    </exclusion>
  </exclusions>
</dependency>

答案 12 :(得分:0)

我和javassist有类似的问题。我在pom.xml文件中使用以下依赖项 -

<org.hibernate.version>4.2.15.Final</org.hibernate.version>
<org.pentaho.di>6.0.1.0-386</org.pentaho.di>


<dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-core</artifactId>
            <version>${org.pentaho.di}</version>
            <exclusions>
                <exclusion>
                    <artifactId>xercesImpl</artifactId>
                    <groupId>xerces</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>javassist</artifactId>
                    <groupId>javassist</groupId>
                </exclusion>
            </exclusions>
        </dependency>



    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${org.hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>ehcache-core</artifactId>
                    <groupId>net.sf.ehcache</groupId>
                </exclusion>
            </exclusions>
        </dependency>

+- org.hibernate:hibernate-ehcache:jar:4.2.15.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
[INFO] |  \- org.hibernate:hibernate-core:jar:4.2.15.Final:compile
[INFO] |     +- antlr:antlr:jar:2.7.7:compile
[INFO] |     +- (org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile - omitted for duplicate)
[INFO] |     +- (dom4j:dom4j:jar:1.6.1:compile - omitted for duplicate)
[INFO] |     +- org.javassist:javassist:jar:3.18.1-GA:compile

Hibernate包含适当的版本,因此将其从pentaho-core中删除 我使用 -

找到了依赖树
mvn dependency:tree -Dverbose >> C:\pipeout2.txt

答案 13 :(得分:0)

如果要在Weblogic服务器或类似服务器上部署基于休眠的应用程序,则服务器的modules文件夹中将具有较早版本的javassist jar。这将导致前面的答案中提到的冲突,您可能最终在类路径中有多个jar。

专门针对weblogic的内容,请在weblogic-application.xml中添加以下内容

添加软件包名称javaassist。*,以确保获取最新版本。

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application
   xmlns:wls="http://www.bea.com/ns/weblogic/weblogic-application"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd http://www.bea.com/ns/weblogic/weblogic-application http://www.bea.com/ns/weblogic/weblogic-application/1.0/weblogic-application.xsd">

   <wls:ejb>
      <wls:start-mdbs-with-application>false
      </wls:start-mdbs-with-application>
   </wls:ejb>

   <wls:prefer-application-packages>
      <wls:package-name>antlr.*</wls:package-name>
      <wls:package-name>org.apache.commons.*</wls:package-name>
      <wls:package-name>org.apache.xmlbeans.*</wls:package-name>
      <wls:package-name>org.springframework.*</wls:package-name>
      <wls:package-name>org.hibernate.*</wls:package-name>
      <wls:package-name>org.joda.*</wls:package-name>
      <wls:package-name>javax.persistence.*</wls:package-name>
      <wls:package-name>com.google.*</wls:package-name>
      <wls:package-name>com.ibm.icu.*</wls:package-name>
      <wls:package-name>org.apache.axiom.*</wls:package-name>
      <wls:package-name>javassist.*</wls:package-name>
   </wls:prefer-application-packages>
</wls:weblogic-application>