30

I want to use hibernate 4.3 for its multitenancy features in JBoss 7.1.

I managed to include it in my war by adding the following lines in jboss-deployment-structure

<exclusions>
   <module name="org.hibernate" />
</exclusions>

and adding a dependency to hibernate core and entity manager in my pom.xml

This made hibernate 4.3 to load but unfortunately I got an error

java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

which is due to JPA 2.0 being loaded when hibernate 4.3 is using JPA 2.1

I have seen these threads and tried what they suggest Excluding JPA Subsystem from JBoss EAP 6.1 - Trying to use JPA 2.1 in JBoss EAP 6.1, JBoss AS7 Automatically Loading JPA, Hibernate 4.3.0.Final & Spring Data JPA 1.4.3.RELEASE.

I added a persistence.xml with

<property name="jboss.as.jpa.managed" value="false" /> 

excluded hibernate jpa 2.0 from Spring Data

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>${spring-data.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Removed JPA subsystem completely from JBoss standalone.xml, without any success.

The only thing that did the trick was to exclude the whole javaee.api in jboss-deployment-structure, as suggested in another thread

<exclusions>
    <module name="javax.persistence.api"/>
    <module name="javaee.api"/>
</exclusions>

but this causes many problems to the rest of my code.

UPDATE: my jboss-deployment-structure.xml is now like this

<jboss-deployment-structure>
<deployment>
    <exclusions>
        <module name="org.slf4j" />
        <module name="org.slf4j.impl" />
        <module name="org.apache.log4j" />
        <module name="javax.persistence.api" />
        <module name="org.hibernate" />
    </exclusions>
    <dependencies>
        <module name="org.jboss.ironjacamar.jdbcadapters" />
        <module name="org.hornetq" />
        <module name="org.hornetq.ra" />
        <module name="org.jboss.ejb3" />
        <module name="org.jboss.ejb-client" />
    </dependencies>
</deployment>
</jboss-deployment-structure>

As you see I have tried many things without luck, so if anyone has another idea it is most welcome.

Community
  • 1
  • 1
andreadi
  • 1,953
  • 1
  • 20
  • 35
  • 1
    In this thread it is discussed why this error is due to JPA 2.1 http://stackoverflow.com/questions/20734540/nosuchmethoderror-in-javax-persistence-table-indexesljavax-persistence-index – andreadi Apr 25 '14 at 13:44
  • try using mvn dependency:tree command and check what is being included – Miron Balcerzak May 02 '14 at 13:53
  • It might help to see the full stack trace and all of the dependencies with their actual versions. – Peter G May 05 '14 at 16:16
  • Haven't found a solution yet. In case anyone is interested, moved to Wildfly and everything works fine. – andreadi May 13 '14 at 14:01
  • After a lot of research and attempts this thread solved my problem: http://stackoverflow.com/questions/31706871/using-jpa-2-1-in-eap-6-4-0/34047877#34047877 – Ronaldo Campos May 09 '17 at 00:04

4 Answers4

8

I would split this problem into two smaller:

1) Make sure you're not transitively depending on JPA 2.0

For this you can use dependency graph visualization tool (NetBeans have one built in, or you can use a maven dependency tree plugin). Another way would be just to skim through the libs, included in your artifact before deploying to JBoss.

2) Ensure correct configuration of JBoss AS 7.1

JBoss AS 7.1 is bundled with Hibernate 4.0.x jars, in order to update them try out this steps as described in the official doc.

  • update the current as7/modules/org/hibernate/main folder to contain the newer version

  • Delete *.index files in as7/modules/org/hibernate/main and as7/modules/org/hibernate/envers/main folders

  • Backup the current contents of as7/modules/org/hibernate in case you make a mistake

  • Remove the older jars and copy new Hibernate jars into as7/modules/org/hibernate/main + as7/modules/org/hibernate/envers/main

  • Update the as7/modules/org/hibernate/main/module.xml + as7/modules/org/hibernate/envers/main/module.xml to name the jars that you copied in

Updated as7/modules/org/hibernate/main/module.xml will look like (note that dependencies won't change):

<?xml version="1.0" encoding="UTF-8"?>

<module xmlns="urn:jboss:module:1.1" name="org.hibernate">
    <resources>
        <resource-root path="hibernate-core-4.3.5.Final.jar"/>
        <resource-root path="hibernate-commons-annotations-4.0.4.Final.jar"/>
        <resource-root path="hibernate-entitymanager-4.3.5.Final.jar"/>
        <resource-root path="hibernate-infinispan-4.3.5.Final.jar"/>
    </resources>

    <dependencies>
       .
       .
    </dependencies>
</module>
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • Have you successfully used something from hibernate 4.3 that uses JPA 2.1? My project seems to have no dependency on JPA 2.0, and still JBoss 7.1 loads it. That is my problem, changing hibernate version is done correctly. – andreadi May 13 '14 at 13:55
  • which version do you change to? – Pablo Johnson Oct 28 '14 at 19:43
  • this **does not work for us: *JBoss 7.1.1.Final* and *Hibernate 4.3.7 Libs* since we get a **`NoClassDefFoundError` for `org/hibernate/service/jta/platform/internal/JBossAppServerJtaPlatform`** which was refactored to another place `org/hibernate/engine/transaction/jta/platform/internal`... http://www.mkyong.com/hibernate/java-lang-classnotfoundexception-org-hibernate-service-jta-platform-spi-jtaplatform/ (your link to the official site mentions JPA 2.0 compatiblity for JB7 and the hibernate upgrade procedure mentioned there is maybe only working for versions below 2.1 support). – Andreas Covidiot Aug 19 '15 at 09:36
  • I have found the reason for that error. In standalone.xml, under the extensions tag, remove `` and the error will disappear. If that line is not removed, Jboss will attempt to load that extension, and if you check the corresponding module for that extension in the module folders, it uses a hibernate-4.0.* jar. – Jadiel de Armas Aug 24 '15 at 17:22
3

Try removing only the javax.persistence.api module along with org.hibernate.

Also, if you want to switch to Hibernate 4.3 for all web applications (which should be backward compatible), switch to a newer version of Hibernate as described here.

Some debugging tips: after generating your JAR files, check explicitly what JPA/Hibernate libraries they contain (by unzipping them). Also in order to check the Hibernate Version you could do it like it is here described.

Also check the structure of the jboss-deployment-structure file, as it seems that the <exclusions> element is neither in a <deployment>, nor in an <sub-deployment> element.

Community
  • 1
  • 1
V G
  • 18,822
  • 6
  • 51
  • 89
  • removing the javax.persistence.api leads to the same error java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index; The method proposed to change the hibernate version, works up to the point where hibernate starts using JPA 2.1. Version 4.3 does so there is a conflict with JPA 2.0 that is enforced somehow by JBoss 7.1 – andreadi Apr 29 '14 at 08:42
  • Let's start debugging: Make the change I proposed to change the hibernate version and start your JBOSS with the JVM option: `-verbose:class`. You should get printed all loaded classes and the JARS where they have been taken from. Identify the `javax.persistence.Table` class please and print that line in a comment below. – V G Apr 29 '14 at 09:00
  • Also make sure there is no wrong JARS with the JPA API, by executing `mvn dependency:tree` – V G Apr 29 '14 at 09:06
  • I haven't changed hibernate for the whole JBoss 7.1, but did only for my project. After starting jboss with -verbose:class, I can see that JPA 2.0 is loaded at the very start of my project deployment even before loading persistence.xml. Then after some time this class is loaded: [Loaded javax.persistence.Table from jar:file:jboss-as-7.1.1.Final/modules/javax/persistence/api/main/hibernate-jpa-2.0-api-1.0.1.Final.jar!/] – andreadi Apr 29 '14 at 10:02
  • 1. Why not changing Hibernate for the whole JBOSS 7.1? 2. Have you tried removing both dependencies `org.hibernate` and `javax.persistence.api`? 3. Show us the entire jboss-deployment-structure.xml file. – V G Apr 29 '14 at 10:13
  • I will try changing the hibernate for the whole JBoss soon, I did before with no luck, since JBoss was complaining from the startup. Both dependencies are now removed. I updated the xml in my question. – andreadi Apr 29 '14 at 10:20
  • Did you try changing it again? – V G Apr 30 '14 at 10:24
0

for us the following helped (works only for non-container-managed JPA scenarios!):

  1. IDE-side: (Eclipse Kepler) removing (added during tests and forgot to remove) the (locally installed) JBoss 7.1.1.Final Library project Build Path dependency

  2. Maven-side: (mvn 3.0.4 and 3.3.3) outcommented the following hibernate JPA 2.0 including section in our global /pom.xml (its an ear project with /pom.xml, ear/pom.xml, ejb/pom.xml and web/pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
        ...
    
        <dependencyManagement>
            <dependencies>
                ...
    
                <!-- outcommented because of JPA 2.1 support -->
                <!-- 
                <dependency>
                    <groupId>org.jboss.bom</groupId>
                    <artifactId>jboss-javaee-6.0-with-hibernate</artifactId>
                    <version>${version.jboss.bom}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                -->
            </dependencies>
        </dependencyManagement>
    
        ...
    
  3. Maven-side: adding only hibernate-entitymanager 4.3.8.Final and our db driver (postgres) as DB/ORM-related dependencies (which load all other required libs as well) in our /ejb/pom.xml

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1102-jdbc41</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.8.Final</version>
    </dependency>
    
  4. server-side: disabling JPA 2.0 in our JBoss 7.1.1.Final globally (for all webapps/libs, if you have multiple) setting <module name="javax.persistence.api" export="false"/>

hints:

Community
  • 1
  • 1
Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96
-2

try using a lower version of the hibernate-entitymanager

 <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.0.Final</version>
</dependency>

and jpa 2.0 may work > It worked for me.

user1419261
  • 829
  • 8
  • 5