Hibernate异常javassist

时间:2017-02-17 12:04:53

标签: eclipse hibernate

我需要一些帮助,因为我对Hibernate 4 / Struts 2项目感到难过。这是我的第一个Hibernate 4(4.3.11)项目,因为我在Hibernate 3工作多年。数据库是MySQL 5.

所有映射类都是使用JBoss Tools 4.3.5在Eclipse Mars 2上提供的Hibernate Tools生成的。没有遇到任何问题,它工作正常。

但是在测试时,我遇到了这个例外:

2017-02-16 17:23:45 ERROR Dispatcher:38 - Exception occurred during processing request: metier.Ville_$$_javassist_2 cannot be cast to javassist.util.proxy.Proxy

java.lang.ClassCastException:metier.Ville _ $$ _ javassist_2无法强制转换为javassist.util.proxy.Proxy     在org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:147)     在org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:75)     在org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:771)

我看了that post,但我不明白发生了什么。

涉及两个班级:Salle和Ville。这是Hibernate XML文件:Salle.hbm.xml& Ville.hbm.xml

<?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 2 f?vr. 2017 11:28:16 by Hibernate Tools 4.3.5.Final -->
<hibernate-mapping>
    <class name="metier.Salle" table="salle" catalog="aevbadherents" optimistic-lock="version">
        <id name="idSalle" type="int">
            <column name="idSALLE" />
            <generator class="assigned" />
        </id>
        <many-to-one name="ville" class="metier.Ville" fetch="select">
            <column name="idVilleSalle" />
        </many-to-one>
        <property name="adresse1" type="string">
            <column name="Adresse1" length="80" />
        </property>
        <property name="adresse2" type="string">
            <column name="Adresse2" length="80" />
        </property>
        <property name="description" type="string">
            <column name="Description" length="500" />
        </property>
        <set name="courses" table="cours" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IdSalle" not-null="true" />
            </key>
            <one-to-many class="metier.Cours" />
        </set>
        <set name="passagegrades" table="passagegrade" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IdSalle" />
            </key>
            <one-to-many class="metier.Passagegrade" />
        </set>
    </class>
</hibernate-mapping>

第二个档案:

<hibernate-mapping>
    <class name="metier.Ville" table="ville" catalog="aevbadherents" optimistic-lock="version">
        <id name="idVille" type="int">
            <column name="idVILLE" />
            <generator class="assigned" />
        </id>
        <many-to-one name="departement" class="metier.Departement" fetch="select">
            <column name="idDepartement" not-null="true" />
        </many-to-one>
        <property name="nom" type="string">
            <column name="Nom" length="60" not-null="true" />
        </property>
        <property name="codepostal" type="string">
            <column name="codepostal" length="6" not-null="true" />
        </property>
        <property name="gpslat" type="string">
            <column name="GPSLat" length="12" not-null="true" />
        </property>
        <property name="gpslon" type="string">
            <column name="GPSLon" length="12" not-null="true" />
        </property>
        <set name="adherentsForIdVilleResid" table="adherent" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IdVilleResid" />
            </key>
            <one-to-many class="metier.Adherent" />
        </set>
        <set name="clubs" table="club" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="idville" not-null="true" />
            </key>
            <one-to-many class="metier.Club" />
        </set>
        <set name="salles" table="salle" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="idVilleSalle" />
            </key>
            <one-to-many class="metier.Salle" />
        </set>
        <set name="adherentsForIdVilleNais" table="adherent" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IdVilleNais" />
            </key>
            <one-to-many class="metier.Adherent" />
        </set>
    </class>
</hibernate-mapping>

我读到这个异常可能是由JAR冲突造成的,但是我没有找到任何东西。我不使用Maven(也许我应该?),所以让我告诉你包含的JAR:

antlr-2.7.7.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.2-javadoc.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
truc.txt
xwork-core-2.3.15.3.jar

请帮助我,因为我很难过......

2 个答案:

答案 0 :(得分:0)

导致此问题的一个可能原因是您在类路径中拥有同一类的多个版本。

我一直在查看java2s.com,你所列出的jar中没有一个似乎包含类javassist.util.proxy.Proxy,所以我将尝试使用以下命令使用jarscan查找是否有任何其他jar包含该类:

jarscan -d PATH_TO_YOUR_CLASSPATH_DIR -j javassist.util.proxy.Proxy

您可以从这里https://java.net/projects/jarscan/downloads

下载jarscan

如果您发现多个软件包包含相同的类,那么您就完成了。

答案 1 :(得分:0)

Malaguna,

感谢您的帮助。这是jarscan的结果:

D:\temp\java\jarscan>jarscan -j javassist.util.proxy.Proxy
......................
+javassist-3.18.1-GA.jar
    javassist-3.18.1-GA.jar\javassist/util/proxy/Proxy.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$1.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$2.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$3.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$ClassLoaderProvide
r.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$Find2MethodsArgs.c
lass
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$ProxyDetails.class

    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$UniqueName.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObject.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObjectInputStream.class
    javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObjectOutputStream.class
----------------------------------------------
Scanned archives: 22
Errors: 0
Archives with hits: 12

因此,这意味着没有其他jar包含该类。