Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OrderBy causing org.hibernate.HibernateException: Unable to parse order-by fragment

I added a @OrderBy on my project and now i'm getting the following error:

17:10:35,125 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 51) MSC000001: Failed to start service jboss.persistenceunit."blueMonster.war#blueMonster": org.jboss.msc.service.StartException in service jboss.persistenceunit."blueMonster.war#blueMonster": javax.persistence.PersistenceException: [PersistenceUnit: blueMonster] Unable to build Hibernate SessionFactory
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:172) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
    at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_40]
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:474)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_40]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_40]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_40]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: blueMonster] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1239) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:855) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
    at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) [jipijapa-hibernate4-3-1.0.1.Final.jar:]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
    ... 8 more
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.collection.OneToManyPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:253) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.persister.internal.PersisterFactoryImpl.createCollectionPersister(PersisterFactoryImpl.java:201) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:430) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
    ... 13 more
Caused by: org.hibernate.HibernateException: Unable to parse order-by fragment
    at org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator.translate(OrderByFragmentTranslator.java:69) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.sql.Template.translateOrderBy(Template.java:728) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.persister.collection.AbstractCollectionPersister.<init>(AbstractCollectionPersister.java:578) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.persister.collection.OneToManyPersister.<init>(OneToManyPersister.java:89) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at sun.reflect.GeneratedConstructorAccessor32.newInstance(Unknown Source) [:1.8.0_40]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.8.0_40]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) [rt.jar:1.8.0_40]
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:231) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    ... 17 more
Caused by: java.lang.ClassCastException: antlr.CommonToken cannot be cast to antlr.Token
    at antlr.CharScanner.makeToken(CharScanner.java:173) [antlr-2.7.7.jar:]
    at org.hibernate.sql.ordering.antlr.GeneratedOrderByLexer.mIDENT(GeneratedOrderByLexer.java:239) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.sql.ordering.antlr.GeneratedOrderByLexer.nextToken(GeneratedOrderByLexer.java:139) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at antlr.TokenBuffer.fill(TokenBuffer.java:69) [antlr-2.7.7.jar:]
    at antlr.TokenBuffer.LA(TokenBuffer.java:80) [antlr-2.7.7.jar:]
    at antlr.LLkParser.LA(LLkParser.java:52) [antlr-2.7.7.jar:]
    at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.expression(GeneratedOrderByFragmentParser.java:565) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.sortKey(GeneratedOrderByFragmentParser.java:346) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.sortSpecification(GeneratedOrderByFragmentParser.java:241) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.orderByFragment(GeneratedOrderByFragmentParser.java:190) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    at org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator.translate(OrderByFragmentTranslator.java:63) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
    ... 24 more

If i remove the @OrderBy it runs fine.

After some digging i found out a workaround, but i cannot use it, the workaround is to change the pom.xml and on the hibernate-core dependency add an exclusion to antl

    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>4.0.1.Final</version>
       <scope>provided</scope>

       <exclusions>
          <exclusion>
             <groupId>antlr</groupId>
             <artifactId>antlr</artifactId>
          </exclusion>
       </exclusions>
    </dependency>

I'm using JPA so i don't have the hibernate-core on my pom.xml My dependencias on pom.xml are:

<dependencies>
        <dependency>
            <groupId>org.jboss.spec.javax.security.auth.message</groupId>
            <artifactId>jboss-jaspi-api_1.1_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.security.jacc</groupId>
            <artifactId>jboss-jacc-api_1.5_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.faces</groupId>
            <artifactId>jboss-jsf-api_2.2_spec</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.interceptor</groupId>
            <artifactId>jboss-interceptors-api_1.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.rmi</groupId>
            <artifactId>jboss-rmi-api_1.0_spec</artifactId>
            <version>1.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.resource</groupId>
            <artifactId>jboss-connector-api_1.7_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.xml.bind</groupId>
            <artifactId>jboss-jaxb-api_2.2_spec</artifactId>
            <version>1.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.xml.rpc</groupId>
            <artifactId>jboss-jaxrpc-api_1.1_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.xml.ws</groupId>
            <artifactId>jboss-jaxws-api_2.2_spec</artifactId>
            <version>2.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.xml.soap</groupId>
            <artifactId>jboss-saaj-api_1.3_spec</artifactId>
            <version>1.0.3.Final</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.jms</groupId>
            <artifactId>jboss-jms-api_2.0_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.management.j2ee</groupId>
            <artifactId>jboss-j2eemgmt-api_1.1_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.jws</groupId>
            <artifactId>jsr181-api</artifactId>
            <version>1.0-MR1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.servlet.jstl</groupId>
            <artifactId>jboss-jstl-api_1.2_spec</artifactId>
            <version>1.1.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.servlet.jsp</groupId>
            <artifactId>jboss-jsp-api_2.3_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.1_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.annotation</groupId>
            <artifactId>jboss-annotations-api_1.2_spec</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.el</groupId>
            <artifactId>jboss-el-api_3.0_spec</artifactId>
            <version>1.0.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.picketbox</groupId>
            <artifactId>picketbox</artifactId>
            <version>4.0.21.Final</version>

            <exclusions>
    <exclusion>
      <groupId>org.picketbox</groupId>
      <artifactId>picketbox-spi-bare</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.picketbox</groupId>
      <artifactId>jbosssx-bare</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging-spi</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.infinispan</groupId>
      <artifactId>infinispan-core</artifactId>
    </exclusion>
  </exclusions>


        </dependency>
        <dependency>
            <groupId>org.picketbox</groupId>
            <artifactId>picketbox-commons</artifactId>
            <version>1.0.0.final</version>
        </dependency>
        <dependency>
            <groupId>org.picketbox</groupId>
            <artifactId>picketbox-infinispan</artifactId>
            <version>4.0.21.Final</version>
        </dependency>
        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-controller-client</artifactId>
            <version>8.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jboss-dmr</artifactId>
            <version>1.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.1.4.GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.0.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>async-http-servlet-3.0</artifactId>
            <version>3.0.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>3.0.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.ejb3</groupId>
            <artifactId>jboss-ejb3-ext-api</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.2</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.10</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-infinispan</artifactId>
            <version>4.2.1.Final</version>

             <exclusions>
    <exclusion>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
    </exclusion>
  </exclusions>


        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.byteslounge</groupId>
            <artifactId>cdi-properties</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.deltaspike.core</groupId>
            <artifactId>deltaspike-core-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.deltaspike.core</groupId>
            <artifactId>deltaspike-core-impl</artifactId>
            <version>1.2.1</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

I'm using Wildfly, how can i solve this issue?

After reading @Guillermo Fernández reply i did the following:

mvn dependency:tree -Dincludes=antlr

The outcome was:

[INFO] blueMonster:blueMonster:war:0.0.1-SNAPSHOT
[INFO] \- org.hibernate:hibernate-infinispan:jar:4.2.1.Final:compile
[INFO]    \- org.hibernate:hibernate-core:jar:4.2.1.Final:compile
[INFO]       \- antlr:antlr:jar:2.7.7:compile

Following his steps i also updated my pom.xml, but something else is also cause the error, since its happening, how do i identify with ones do i have to exclude?

My @OrderBy

@OrderBy("data") 
@OneToMany(cascade = CascadeType.ALL, mappedBy = "csr")
    private List<CsrSeveridadeLog> severidadeLogList;
like image 392
prabello Avatar asked Mar 23 '15 20:03

prabello


2 Answers

After several attempts with exclusions, I solved adding

-DANTLR_USE_DIRECT_CLASS_LOADING=true

to JAVA_OPTS in JBoss launching script as suggested here.

like image 147
Linuslabo Avatar answered Nov 16 '22 16:11

Linuslabo


First at all, about the workaround, take into account that the hibernate-core version you are using is 4.3 (as you can see in the error's stacktrace) not the 4.0.1.

In the simplest scenario, the workaround means that your app includes an antlr's jar. By default, the module org.hibernate that Wildfly AS loads cause the loading of the org.antlr module's jars. The explicit dependecy doesn't appear in your pom so maybe it is implicit referenced by another dependecy or the jar is already in the app's project and then packaged within.

I think you should review and fix the dependencies and/or clean the jars archives but if you really can't do it I suggest you to try to exclude the antlr module using the corresponding deployment descriptor that you will must package into your app. Sorry I don't have any example of descriptor to show you right now. Read this and this doc to know how to write it and to learn about classloading and Jboss AS modules dependencies. Don't worry about the AS version referenced in these links, respect of this issue applies pretty much the same.

Improve: To find out which dependency includes antlr you can use maven dependecy plugin. I run this command line on your list of dependecies:

mvn dependency:tree -Dincludes=antlr

The output is:

[INFO] \- org.hibernate:hibernate-infinispan:jar:4.2.1.Final:compile
[INFO]    \- org.hibernate:hibernate-core:jar:4.2.1.Final:compile
[INFO]       \- antlr:antlr:jar:2.7.7:compile

I am guessing but you are packaging your web application within the dependencies you list, because there is not parent pom or referenced bom that set scope other than compile. In that case it is really a mess, I suggest to you to use Wildfly BOMs to resolve the version of the dependencies that you app needs (read about here and here to know how to use/add it) and begin for setting scope provided to all dependencies that Wildfly provides (so they are listed in the BOM). Next, maybe you need to declare explicit reference to some Wildfly's module to include the jars/dependencies in your classpath's app that aren't includes automatically or by default, but I thinks it is for another question.

About the workaround, in your case you should add something like this (according to the pom of Wildfly project) to exclude the antlr jar, and probably something else is missing too.

<dependency>
  <groupId>org.picketbox</groupId>
  <artifactId>picketbox-infinispan</artifactId>
  <version>4.0.21.Final</version>
  <exclusions>
    <exclusion>
      <groupId>org.picketbox</groupId>
      <artifactId>picketbox-spi-bare</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.picketbox</groupId>
      <artifactId>jbosssx-bare</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging-spi</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.infinispan</groupId>
      <artifactId>infinispan-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-infinispan</artifactId>
  <version>4.2.1.Final</version>
  <exclusions>
    <exclusion>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
    </exclusion>
  </exclusions>
</dependency>
like image 44
Guillermo Avatar answered Nov 16 '22 16:11

Guillermo