Hibernate NoSuchMethodError for addPackage method - java

i use Hibernate for connecting to my db and i configure my database in side of my dbConnector class:
public static SessionFactory connectingHibernate(){
Properties database=new Properties();
database.setProperty("hibernate.connection.driver_class",PROPERTY_NAME_DATABASE_DRIVER);
database.setProperty("hibernate.connection.username",USERNAME);
database.setProperty("hibernate.connection.password",PASSWORD);
database.setProperty("hibernate.connection.url",url);
database.setProperty("hibernate.dialect",DIALECT);
Configuration cfg=new Configuration()
.setProperties(database)
.addPackage("androidapi.model")
.addAnnotatedClass(User.class)
.addAnnotatedClass(Product.class)
.addAnnotatedClass(PriceProduct.class)
.addAnnotatedClass(CoinPrice.class)
.addAnnotatedClass(SellPage.class)
.addAnnotatedClass(Setting.class)
.addAnnotatedClass(Message.class);
StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties());
sessionFactory=cfg.buildSessionFactory(ssrb.build());
return sessionFactory;
}
when i run my application from inside of my ide my code works correctly but after making war file with maven i got this exception on methods that use my db! how can i fix this?
exception:
"error": "Internal Server Error",
"exception": "java.lang.NoSuchMethodError",
"message": "org.hibernate.cfg.Configuration.addPackage(Ljava/lang/String;)Lorg/hibernate/cfg/Configuration;",
after removeing this line: .addPackage("androidapi.model") i got this exception:
"exception": "java.lang.NoSuchMethodError",
"message": "org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;",
"path": "/myinsta/admin_api/mGetAllUser"
my pom.xml Hibernate dependencies:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>LATEST</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>LATEST</version>
</dependency>
solved! read answer and its good to know that you should use compatible version of hibernate that can find here at meaven part:
Maven dependency

You have multiple versions of hibernate on your classpath (at the time of this writing this: most likely 3.6.0.beta2 and at 5.2.1.Final)
Remove:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>LATEST</version>
<type>pom</type>
</dependency>
This is pulling in an older version.

You don't need this line
addPackage("androidapi.model")
The method addPackage() doesn't scan a package. It is for reading "package-level metadata" (.package-info- files).
Looks like, you use Hibernate 5. You can't use Hibernate 4 configuration code with Hibernate 5
https://stackoverflow.com/a/32711654/3405171
If you want to scan packages
https://stackoverflow.com/a/35248061/3405171
To use a valid Hibernate version just check this page with Hibernate core
versions:
https://mvnrepository.com/artifact/org.hibernate/hibernate-core
and add it to the pom.xml (if you use Maven). For example for Hibernate 5.2.1.Final (It uses Java 8):
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.1.Final</version>
</dependency>
A dependency from hibernate-core is pretty enough, because of all other libraries will be loaded by Maven as transitive dependences. However, to use logger you need to add an additional dependency.
If you want to download an archive with all required jars, you can check the Hibernate download page:
http://hibernate.org/orm/downloads/

Related

Unable to integrate Hazelcast version 4.2 with Spring Boot 2.3.6.RELEASE

I am using Spring Boot version 2.3.6.RELEASE in my application and added hazelcast version 4.2 and wanted to use spring-integration-hazelcast of version 3.0.0 as this only is compatible with hazelcast 4.2 version.
But getting below error during build:
Require upper bound dependencies error for org.springframework.integration:spring-integration-core:5.3.4.RELEASE paths to dependency are:
+-package:ocapi-admin-service:4.0.0-SNAPSHOT
+-org.springframework.integration:spring-integration-core:5.3.4.RELEASE
and
+-package:ocapi-admin-service:4.0.0-SNAPSHOT
+-org.springframework.integration:spring-integration-hazelcast:3.0.0
+-org.springframework.integration:spring-integration-core:5.3.4.RELEASE (managed) <-- org.springframework.integration:spring-integration-core:5.4.0
]
Below is the pom snippet:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-hazelcast</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>4.2</version>
</dependency>
I think you need to update the version of spring-integration-core to at least 5.4.0.
You don't specify it in the code snippet from pom.xml you shared, but I guess it must be taken from somewhere else since you have it in the logs. Try changing this dependency to the following one:
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>5.4.0</version>
</dependency>

attempting to get data from indexed table produces -java.lang.NoSuchMethodError: org.hibernate.query.internal.ParameterMetadataImpl

I am attempting to retrieve data from hibernate search, after indexing the table and I attempt to make a search I get this error even when everything looks perfect to me. I have googled and found this solution
How to fix "NoSuchMethodError ParameterMetadataImpl <init> but still did not provide a headway to solving the problem-
java.lang.NoSuchMethodError: org.hibernate.query.internal.ParameterMetadataImpl.([Lorg/hibernate/engine/query/spi/OrdinalParameterDescriptor;Ljava/util/Map;)V
here is my complete pom.xml file
<!--<dependency>-->
<!--<groupId>org.hibernate</groupId>-->
<!--<artifactId>hibernate-search-orm</artifactId>-->
<!--<version>5.6.0.Final</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.8.2.Final</version>
</dependency>
</project>
kindly assist
The version of Hibernate Search you're using is simply too old, and not compatible with the version of Hibernate ORM provided by Spring Boot.
You should check the compatibility matrix on the Hibernate website and pick the Hibernate Search version accordingly.
Spring Boot 2.1.4.RELEASE uses Hibernate ORM 5.3.9.Final. According to the compatibility matrix, with Hibernate ORM 5.3.x, you should use Hibernate Search 5.10.x. As indicated here, the latest release in the 5.10 series is currently 5.10.9.Final.
So, change this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.8.2.Final</version>
</dependency>
To this:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.10.9.Final</version>
</dependency>

Use Provided jax-rs 2.0 and Jersey 2.1x in Weblogic 12c (12.2.1.3)

I need to implement a rest service call in my WEB application. According to Oracle, Weblogic is supported and does not need to register (deploy) jax-rs, so I would like to use these Server libraries. I made a simple class by calling a service (get). I configured the dependencies in the project and deployed it on Weblogic. However, when deploying, the following error appears: java.lang.ClassCastException: Cannot cast org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider to org.glassfish.jersey.server.spi.ComponentProvider
Note: It worked using this link below (deploying the jar on the server) But I want to use the native libraries on Weblogic. Could someone help me please?
https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297
Code example (Java)
String host = "https://swapi.dev/api/people/2/";
Client client = ClientBuilder.newBuilder().build();
WebTarget webTarget = client.target(host);
Builder builder = webTarget.request(MediaType.APPLICATION_JSON);
String result = builder.get(String.class);
pom.xml
<properties>
<primefaces.version>3.5.RC1</primefaces.version>
<jersey.version>2.21.1</jersey.version>
<jaxrs.version>2.0</jaxrs.version>
</properties>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
<scope>provided</scope>
</dependency>
<!-- Jersey 2.21.1 -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
weblogic.xml
<wls:weblogic-version>12.2.1.3</wls:weblogic-version>
<wls:context-root>RecebimentoMercadoriaWEB</wls:context-root>
<wls:library-ref>
<wls:library-name>jsf</wls:library-name>
</wls:library-ref>
<wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
</wls:container-descriptor>
<wls:session-descriptor>
<wls:cookie-name>CookieRecebimentoMercadoria</wls:cookie-name>
</wls:session-descriptor>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>javax.faces.*</wls:package-name>
<wls:package-name>com.sun.faces.*</wls:package-name>
<wls:package-name>com.sun.facelets.*</wls:package-name>
<wls:package-name>com.bea.faces.*</wls:package-name>
</wls:prefer-application-packages>
<wls:prefer-application-resources>
<wls:resource-name>javax.faces.*</wls:resource-name>
<wls:resource-name>com.sun.faces.*</wls:resource-name>
<wls:resource-name>com.sun.facelets.*</wls:resource-name>
<wls:resource-name>com.bea.faces.*</wls:resource-name>
<wls:resource-name>META-INF/services/javax.servlet.ServletContainerInitializer</wls:resource-name>
<wls:resource-name>META-INF/services/com.sun.faces.spi.FacesConfigResourceProvider</wls:resource-name>
</wls:prefer-application-resources>
</wls:container-descriptor>
On Weblogic 12.2.1.3 yo do not need to execute the procedure described by the link you have pointed, I mean the link below .
https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297
This is because that link belongs to Oracle Weblogic 12.1.3 and there are several differences between Weblogic 12.1.3.0 and Weblogic 12.2.1.3.
Furthermore, this document for Oracle Weblogic 12.2.1.3 states.
Note:
Jersey 2.x (JAX-RS 2.0 RI) support is provided by default in this
release of WebLogic Server. Registration as a shared library is no
longer required.
This means, when it comes to Weblogic 12.2.1.3 Jersey libraries are in place and ready to be used. Thus, your application should be able to use them.
However, I think server libraries are getting troubles with the libraries you are using within your pom.xml file.
Furthermore Oracle Weblogic 12.2.1.3 provides jersey 2.22.4
I have also used wls-cat in one of my servers to know, which library is loading the class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider and I have found the library in $ORACLE_HOME/oracle_common/modules/org.glassfish.jersey.ext.cdi.jersey-cdi1x.jar, which means it is loaded by Weblogic as is stated on above documentation.
Furthermore, after running wls-cat I can see this:
org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider implements org.glassfish.jersey.server.spi.ComponentProvider
Thus, cast should not be an issue, which means there is a class loading problem that most probably is caused by libraries included in your application.
You can see the results of wls-cat executed on my server on below picture
You can use wls-cat to see which file (a JAR library) is loading the conflicting class. In below post you will find information about how to use wls-cat to analyse class loading problems.
https://blog.sysco.no/class/loader/AnalysingClassLoadingConflicts/
The libraries were really conflicting. I removed these dependencies from pom.xml and it worked. Thanks for the tip.
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.1</version>
<scope>provided</scope>
</dependency>

org.hibernate.internal.util.xml.XMLHelper.<init>(Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService but it does not exist

An attempt was made to call the method org.hibernate.internal.util.xml.XMLHelper.(Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)V but it does not exist. Its class, org.hibernate.internal.util.xml.XMLHelper, is available from the following locations:
I had a same situation with following dependencies. And I did this.
Exclude hibernate-envers from spring-data-envers.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>${version.org.springframework.data}</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
</exclusion>
</exclusions>
</dependency>
And depends on it directly.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${version.org.hibernate}</version>
<scope>compile</scope>
</dependency>
I had the same issue migrating from Spring Boot 2.1.3 to 2.1.4,
There are some major changes between Hibernate 5.3 and 5.4, so you need to make sure that all of your dependencies are using this last version.
Solution : update your dependencies to versions using Hibernate 5.4 (In my case I had to update hibernate-jpamodelgen to the last version).
(Maybe you could also use exclusions to prevent your dependencies using the wrong versions : https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html)

java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName

JDK: 1.8.0_131
Tomcat: 8.0.27.0
Hibernate Validator: 6.0.7.Final + all the dependencies downloaded from: Hibernate Validator 6
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public AccountSyncResponse excute(AccountSyncRequest account_sync_request_)
{
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<AccountSyncRequest>> violations = validator.validate(account_sync_request_);
.
.
.
.
AccountSyncResponse _AccountSyncResponse = new AccountSyncResponse();
return _AccountSyncResponse;
}
The code fail on Validation.buildDefaultValidatorFactory() with the exception:
java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String;
at org.hibernate.validator.internal.xml.ValidationBootstrapParameters.<init>(ValidationBootstrapParameters.java:63)
at org.hibernate.validator.internal.engine.ConfigurationImpl.parseValidationXml(ConfigurationImpl.java:527)
at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:328)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)
It looks like the wrong jar file is being used but I can’t figure out which one.
I had this same issue after upgrading from Springboot 1.5.x to Springboot2. The solution was to upgrade Java EE 7 to Java EE 8:
From:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
To:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
Spring Boot 2 comes with hibernate-validator 6 (org.hibernate.validator:hibernate-validator:6.0.16.Final, which depends on validation-api 2 (javax.validation:validation-api:2.0.1.Final), which is specific for Java EE 8, see Appendix F. Dependency versions. But it may happen that you have to support older application servers with Java EE 7 only. Spring Framework 5 should still suport it, see runtime support.
In that case, use older hibernate-validator (5.4.3.Final) and validation-api (1.1.0.Final). If you use Spring Boot maven parent, just define these properties.
<properties>
<javax-validation.version>1.1.0.Final</javax-validation.version>
<hibernate-validator.version>5.4.3.Final</hibernate-validator.version>
</properties>
The problem is that hibernate-validator has changed the groupId since version 6, so you have to exclude the new group but add the old one, e.g.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
I've just added validation-api dependency to my pom.xml and it works!
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

Categories