休眠异常 _$$_javassist_0 无法转换为 javassist.util.proxy.Proxy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22481540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:00:12  来源:igfitidea点击:

Hibernate exception _$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy

javahibernatejakarta-eeormproxy

提问by Sergey Pekar

Hello I am using developnig java web application and I am getting the next exception when I am trying to fetch data using hibernate

您好,我正在使用 developmentnig java web 应用程序,当我尝试使用 hibernate 获取数据时遇到下一个异常

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

Here 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)

Here my code

这是我的代码

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 {

    }
}

Here is my objects

这是我的对象

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;
    }

}

here is mappings

这是映射

<!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>

I am using Oracle Weblogic 12c server

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

采纳答案by Sergey Pekar

Fixed by changing dependency in my pom.xml to older version 4.3.4.Final Error was in 4.3.4.Final version changed to 4.2.7.Final

通过将我的 pom.xml 中的依赖项更改为旧版本 4.3.4.Final 来修复错误是在 4.3.4.Final 版本更改为4.2.7.Final

Do not think it is the best solution but I have not found any other.

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

回答by Wundwin Born

Check your return type Tor List<T>of results

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

回答by anquegi

I use with hibernate 4.3.5.Final and have similar problem with javassist, the problem is that javassist is missing, and you can get the latest from the Maven repositorty.

我与 hibernate 4.3.5.Final 一起使用,并且与 javassist 有类似的问题,问题是缺少 javassist,您可以从 Maven 存储库中获取最新版本。

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

In later editions it is fixed again

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

With this all works in my case,

在我的情况下,这一切都有效,

回答by rand0m86

In my case problem was that there were two javassistlibraries in classpath. One from org.hibernateand other from org.apache.struts.xwork. Solved by removing latter.

就我而言,问题是javassist类路径中有两个库。一个来自org.hibernate另一个来自org.apache.struts.xwork。通过删除后者解决。

回答by Volodymyr Shynkliar

It looks that Weblogic itself contains javaassist older, than your application has. It can be fixed in application descriptor, weblogic.xml or weblogic-application.xml. Just add

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

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

into root config element.

进入根配置元素。

Btw, you can check such conflicts using Weblogic Classloader Analysis Tool - it can be accessed on /wls-cat context path of your server.

顺便说一句,您可以使用 Weblogic 类加载器分析工具检查此类冲突 - 它可以在您服务器的 /wls-cat 上下文路径上访问。

回答by Paulo Jr

Like anquegi has already commented. You have a lib conflict. Back hibernate to 4.2.7.Final will not solve your lib conflict. Run mvn dependency:tree -Dverboseand look to javassist different versions. In my case, I need to exclude javassist from Hibernate dependencies.

就像 anquegi 已经评论过一样。你有一个库冲突。将休眠返回到 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>

This may help others in to future.

这可能会帮助其他人在未来。

回答by rasolog

I had the same problem with Spring Data JPA. I confirm it comes from javassit conflict. The solution is :

我对 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>

回答by Peter Kirby

While only partially related to your question, I wanted to post this somewhere to help out anyone who may come across this problem when using SpringMVC, Hibernate, and Apache Tiles.

虽然仅与您的问题部分相关,但我想将其发布在某处以帮助在使用 SpringMVC、Hibernate 和 Apache Tiles 时可能遇到此问题的任何人。

I was getting this exception when I had org.apache.tiles tiles-extras listed as a dependency. Tiles-extras and hibernate-core have different versions of javassist listed as a dependency. By adding the following to my pom.xml file, I was able to fix the error.

当我将 org.apache.tilestiles-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>

回答by Kisanagaram

I have been using 4.2.8-Final version of hibernate and was having this issue and went through the below answers/suggestions earlier. Here is the sequence of events I made - To get rid of the issue with 4.2.8-Final, was using "parent classes last" class loader approach in Websphere. Recently when the project grew in size (by having more hibernate related jar flavors), started to get the same issue.

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

Finally excluded the "javassist" from 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>

and added

并添加

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

Even this didn't resolve the problem completely (still I had to use 'parent last' class loader approach with hibernate to get rid of the issue.

即使这样也没有完全解决问题(我仍然必须使用带有休眠的“父最后一个”类加载器方法来解决这个问题。

Finally I downgraded hibernate version from 4.2.8-Final to 4.2.7-Final and the issue is solved (I need not change my class loader preference in websphere admin console as parent last anymore)

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

Not exactly sure what change in 4.2.8 Hibernate is causing this issue. Curious to know.

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

回答by rgrebski

I had similar issue on JBoss EAP 7.1.3 (EAP 6.0.1) for hibernate 4.2.x. Its because JBoss modules already contains javassist lib.

我在 Hibernate 4.2.x 的 JBoss EAP 7.1.3 (EAP 6.0.1) 上遇到了类似的问题。这是因为 JBoss 模块已经包含 javassist 库。

I solved it by creating jboss-deployment-structure.xmlin WEB-INF directory with such content:

我通过在具有以下内容的 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>