How to use HibernateValidator with Java Modular System and JavaSE - jpa

I have a very simple JPA project based on eclipselink. All subprojects have module-info files, so they are java modules. And I have a huge problem with Hibernate Validator integration:
jakarta.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 3.0.2.v202107160933): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [TEST_RENT_PU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-30014] (Eclipse Persistence Services - 3.0.2.v202107160933): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: The persistence unit specifies validation-mode as "CALLBACK" but a Bean Validation ValidatorFactory could not be initialized. Please refer to the nested exception for details. Please ensure that Bean Validation API and Bean Validation Provider is available in classpath.
Internal Exception: java.util.ServiceConfigurationError: jakarta.validation.spi.ValidationProvider: module jakarta.validation does not declare uses
without module-info.java file everything works perfectly (all required libraries are added to pom.xml). As you can see I changed validation settings in persistence.xml file to CALLBACK. With AUTO setting bean validation does not work at all. How should I change the project configuration to make Hibernate Validator working in JPMS environment?

Related

javax.persistence jar is not loaded into tomcat

I'm trying to run JPA servlet on Tomcat 8.
From my servlet I call method of a class named QueryDB.class located in another Eclipse project (JPA project, not WEB project).
But tomcat throws
SEVERE: Servlet.service() for servlet [MyRESTService] in context with path [/AutomationWeb] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/persistence/Persistence] with root cause
java.lang.ClassNotFoundException: javax.persistence.Persistence
at java.net.URLClassLoader$1.run(Unknown Source)
...
Exception is thrown at line EntityManagerFactory emf = Persistence.createEntityManagerFactory( "AutomationDB_JPA" ); in QueryDB.class
I'm using Eclipse Mars with Maven. I have EclipseLink in my pom.xml defined:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.1</version>
</dependency>
I have following jars in my Application's WEB-INF/lib:
eclipselink-2.6.1.jar
javax.persistence-2.1.0.jar , it has javax/persistence/Persistence.class inside
There is no other SEVERE messages in the tomcat log.
What's wrong with this setup?
I also tried adding the persistence jar to apache-tomcat-8.0.28\lib , but got the same error.
Update: I moved Persistence.createEntityManagerFactory call to the servlet itself and got no classloader exception. So the question changes: why classloader used for QueryDB.class is different from servlet's classloader? Also, why classloader used in QueryDB.class does not see the Persistence.class, which is seen by the servlet's classloader?
Ok I've found a solution.
Excluded my JPA project from tomcat VM classpath
Converted my JPA project to the Utility project
Modified Deployment Assembly in my Web project properties so that it
will accept JPA project as .jar archive into WEB-INF/lib
I have no classloader exception anymore.

Not loading the persistence provider class in runtime

I have created RestService enabled with JPA(Generic 2.0 version). Using jersey jars and hibernate entity manager jar as dependencies.
Deployment is successfull in tomcat. But when any transaction is processed, getting below exception in runtime.
java.lang.ClassNotFoundException: javax.persistence.spi.PersistenceProvider
What could be the issue, not loading the persistence provider class even it is in the classpath ?
What do you mean with classpath? The library/jar of the Persistence Provider Hibernate and the JPA (javax.persistence) have to be in the ear/war file so that Tomcat is able to access them.
How do you create that ear/war file? With Maven? Then you have to set the dependencies at least to runtime.

Jboss as7 to WildFly migration weld unsatisfied dependencies for type

I'm trying to migrate a jboss as 7 application to WildFly.
For some reason I am getting the weld unsatisfied dependencies for type Set.
For just about every class that uses the #Inject (Using guice).
I can not think of any way for solving this and have been stuck for several hours now.
Exception 0:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type 'classNameHere' with qualifires #Default
at injection point [BackedAnnotatedField] #Inject cant.tell.du.services.data.syly.nbp.syll.gkfsn
at cant.tell.du.services.data.syly.nbp.syll.gkfsn(syll.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProbloms(Validator.java:368)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:289)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:135)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:166)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:514)
Ok, now I got the point that you are using guice instead of cdi.
You could try to exclude the weld subsystem from your deployment like so:
<exclude-subsystems>
<subsystem name="weld" />
</exclude-subsystems>
This should disable CDI for your deployment and you should not run into the same problems anymore.

How to debug NoSuchMethodError exception ? org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)

When I try to initialize a Hibernate 4 SessionFactory in a servlet:
Configuration config;
ServiceRegistry registry;
SessionFactory factory;
config = new Configuration();
config = config.addAnnotatedClass(Star.class); // <-- Exception here.
I get an exception:
SEVERE: Servlet.service() for servlet [hu.adamsan.store.TestHibernate] in context with path [/TestProject] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
at hu.adamsan.store.TestHibernate.doGet(TestHibernate.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at
When I looked up in Google, I could not find a solution, but similar errors were present when there was a mismatch with the hibernate version. I've made a similar project, with the exact same maven dependencies, with the same code initializing hibernate in a doGet method, and it worked.
maven dependencies:
mysql-connector-java 5.1.26
hibernate-core 4.2.6.Final
hibernate-validator 5.0.1.Final
commons-beanutils 1.8.3
commons-collections 3.2.1
log4j 1.2.17
slf4j-api 1.7.5
Does anyone have an idea, what could be wrong?
Can someone offer general advice, how to handle, debug such obscure errors?
When you get a NoSuchMethodError in external libraries, it is usually due to having multiple versions of the same dependency on the classpath. This might be due to a transitive dependency, so detecting it through just looking at the dependency in the pom.xml of your project is not enough. If you do a mvn dependency:tree, it will list all transitive dependencies as well, so look for multiple versions of the same dependency.
It might also be that the same class is implemented is multiple dependencies, so google it to find out which jars contain it. http://www.jarfinder.com/index.php/java/info/org.hibernate.cfg.Configuration

Deploying JPA 2 (OpenJPA) on Jboss 7.1.0-Final-Snapshot

I have deployed axis2 on Jboss 7.1.0 Final Snapshot. We have a service (.aar) which uses OPenJPA 2.1.
All the OpenJPA jars are packaged within the service archive.
When I start the server I see this error message
Constructor threw exception; nested exception is java.lang.RuntimeException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named example
I have defined the provider in the persistence.xml as org.apache.openjpa.persistence.PersistenceProviderImpl
Structure of my Service is as follows,
SampleOpenJPAService.aar
|-lib
|-myJPAClasses.jar
Can anyone help me in understanding What is going wrong here.
regards,
sathwik