Hibernate “PreInsertEvent.getSource()” NoSuchMetho

2020-02-13 03:35发布

问题:

I'm recieving the following error when trying to do inserts:

java.lang.NoSuchMethodError : org.hibernate.event.PreInsertEvent.getSource()Lorg/hibernate/event/EventSource;

I've seen other people with the same problem due to incompatibility in hibernate jars, but I believe I've got it right (according to the compatibility matrix)

Here's the relevant section from my pom.xml:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>3.1.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.4.0.GA</version>
    </dependency>

Can anyone advise?

Regards

Marty

回答1:

I found a solution, but I'm not sure it's correct -- anyone with a better, please advise:

Added a reference to cglib, and explicity excluded hibernate (was including 3.2)

 <dependencies>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>3.1.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.4.0.GA</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>
</dependencies>


回答2:

This is a known bug : https://hibernate.onjira.com/browse/HVAL-81 . It occurs when you reference an older version of hibernate validator than core.



回答3:

The actual problem for me when this error occurred is

  1. Hibernate-core dependency was not in my EAR packaging.

  2. By default it was picking the jboss.4.2.3/.../lib's hibernate3.jar.

  3. Just adding hibernate-core-3.3.1.GA to my dependencies list in EAR packaging.

  4. Already had the overriding of loaders set in jboss-app.xml.

  5. Excluded the hibernate-core from hibernate-entitymanager-3.4.0.GA (don't think this is required as the core supplied will be 3.3.0.SP1 and will be omitted anyhow).

It worked with some exclusions of some already existing xml-apis, ejb3-persistence, etc. dependecies from the hibernate-core.

Finally the core dependency looked like this.

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.1.GA</version>
          <exclusions>
            <exclusion>
                <artifactId>ejb3-persistence</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jta</artifactId>
                <groupId>javax.transaction</groupId>
            </exclusion>
            <exclusion>
                <artifactId>persistence-api</artifactId>
                <groupId>javax.persistence</groupId>
            </exclusion>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Note: I don't think cglib is required is its not relevant to this context.

Hope this is useful for someone.