Issue
I have a problem that I cannot solve. I need your assistance to solve the problem. I have two projects, “sampleZkProject” and “institutiongeography”. Both is maven project. They run on Java 8.
“sampleZkProject” is a sample zk project that is responsible from the web tier. It needs to interact with institutiongeography. institutiongeography is a pom type maven project. Institutiongeography uses hibernate as an ORM framework. Institutiongeography has passed the tests, so we know with a confidence interval that it works well.
“sampleZkProject” s pom structure is as follows:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>experimental.tahasozgen.zk</groupId>
<artifactId>ornek1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<zk.version>9.6.0</zk.version>
<commons-io>2.6</commons-io>
<maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
<packname>-${project.version}-FL-${maven.build.timestamp}</packname>
</properties>
<packaging>war</packaging>
<name>The ornek1 Project</name>
<description>The ornek1 Project</description>
<licenses>
<license>
<name>GNU LESSER GENERAL PUBLIC LICENSE, Version 3</name>
<url>https://www.gnu.org/licenses/lgpl.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<repositories>
<repository>
<id>ZK CE</id>
<name>ZK CE Repository</name>
<url>https://mavensync.zkoss.org/maven2</url>
</repository>
<repository>
<id>ZK EVAL</id>
<name>ZK Evaluation Repository</name>
<url>https://mavensync.zkoss.org/eval</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>zkmaven</id>
<name>ZK Maven Plugin Repository</name>
<url>https://mavensync.zkoss.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkbind</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zul</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zkplus</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zk</groupId>
<artifactId>zhtml</artifactId>
<version>${zk.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io}</version>
</dependency>
<!--other dependencies-->
<dependency>
<groupId>com.tahasozgen.institutiongeography</groupId>
<artifactId>institutiongeography-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tahasozgen.institutiongeography</groupId>
<artifactId>institutiongeography-entities</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tahasozgen.institutiongeography</groupId>
<artifactId>institutiongeography-repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tahasozgen.institutiongeography</groupId>
<artifactId>institutiongeography-businesslayer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tahasozgen.institutiongeography</groupId>
<artifactId>institutiongeography-contracts</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.tahasozgen.institutiongeography</groupId>
<artifactId>institutiongeography-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.27.Final</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Run with Jetty -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>10.0.5</version>
<configuration>
<webApp>
<contextPath>/${project.artifactId}</contextPath>
<allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
</webApp>
<scanIntervalSeconds>5</scanIntervalSeconds>
</configuration>
</plugin>
<!-- Compile java -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!-- Build war -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.3.1</version>
</plugin>
<!-- Pack zips -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>webapp</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>ornek1${packname}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/webapp.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
sampleZkProject has a class that is responsible to interact with institutiongeography. This class’ structure is as follows:
package experimental.tahasozgen.zk.ornek1;
import java.util.HashMap;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Destroy;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import experimental.tahasozgen.institutiongeography.contracts.abstracts.institutionmanagement.InstitutionService;
public class MyViewModel {
private InstitutionService institutionService;
private int count;
@Init
public void init() {
count = 100;
institutionService = experimental.tahasozgen.institutiongeography.service.dependencymanagement.DependencyResolver.getSample().resolve(InstitutionService.class);
System.out.println(String.format("is institutionService empty? %b", institutionService ==null));
}
/*other functions*/
}
experimental.tahasozgen.institutiongeography.service.dependencymanagement.DependencyResolver.getSample().resolve(Class classInstance)
is a java function that resolves the InstitutionService at runtime. When it tries to resolve it, it runs the below:
private EntityManagerFactory createEntityManagerFactory() {
return new EntityManagerFactory();
}
The definition of the EntityManagerFactory is as follows:
package experimental.tahasozgen.institutiongeography.businesslayer.dependencyresolvers;
package experimental.tahasozgen.institutiongeography.businesslayer.dependencyresolvers;
//import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.LinkedBlockingQueue;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.jpa.HibernatePersistenceProvider;
import experimental.tahasozgen.institutiongeography.core.crosscuttingconcern.exceptions.ExceptionAffairs;
import experimental.tahasozgen.institutiongeography.core.dataccess.entityframework.IEntityManagerFactory;
class EntityManagerFactory implements IEntityManagerFactory {
private static EntityManagerFactory factory;
private List<EntityManager> activeEntityManagerList;
private List<EntityManager> closedEntityManagerList;
static {
try {
if (factory == null) {
factory = createEntityManagerFactory();
}
} catch (ExceptionInInitializerError hata) {
throw hata;
}
}
public EntityManagerFabrikasi() {
activeEntityManagerList = new ArrayList <EntityManager>();
closedEntityManagerList = new ArrayList <EntityManager>();
}
@Override
public EntityManager createEntityManagerFactory () throws Exception {
try {
EntityManager entityManager = factory.createEntityManager();
this.activeEntityManagerList.add(entityManager);
return entityManager;
} catch (Exception hata ) {
throw hata;
}
}
@Override
public void close() throws Exception {
if(this.activeEntityManagerList!= null) {
for(EntityManager entityManager : this.activeEntityManagerList) {
if(entityManager == null )
continue;
entityManager.close();
}
}
}
@Override
public void close ( EntityManager entityManager ) {
if(entityManager == null)
return;
entityManager.clear();
entityManager.close();
this.activeEntityManagerList.remove(entityManager);
this.closedEntityManagerList.add(entityManager);
}
private static synchronized EntityManagerFactory createEntityManagerFactory() {
EntityManagerFactory entityManagerFactory = new HibernatePersistenceProvider()
.createContainerEntityManagerFactory(PersistenceUnitInfoIsleri.olusturPersistenceUnitInfo(),
getProperties());
return entityManagerFactory;
}
private static Map<String, String> getProperties() {
try {
Map<String, String> result = new HashMap<String, String>();
Properties properties = new Properties();
String databasaConnectionDetailsFile = "details.txt";
try (InputStream inputStream = EntityManagerFabrikasi.class.getClassLoader()
.getResourceAsStream(databasaConnectionDetailsFile)) {
if (inputStream != null)
properties.load(inputStream);
else
throw new Exception("Even if nothing happened, something definitely happened.");
}
Set<String> keys = properties.stringPropertyNames();
for (String key : keys)
result.put(key, properties.getProperty(key));
return result;
} catch (Exception hata) {
ExceptionAffairs exceptionAffairs = new ExceptionAffairs();
exceptionAffairs.report(hata);
}
return new HashMap<String, String>();
}
}
Here is the pom structure of the maven module with EntityManagerFactory:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>experimental.tahasozgen</groupId>
<artifactId>institutiongeography</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>institutiongeography-businesslayer</artifactId>
<name>institutiongeography-businesslayer</name>
<dependencies>
<dependency>
<groupId>experimental.tahasozgen</groupId>
<artifactId>institutiongeography-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>experimental.tahasozgen</groupId>
<artifactId>institutiongeography-contracts</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>experimental.tahasozgen</groupId>
<artifactId>institutiongeography-repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>experimental.tahasozgen</groupId>
<artifactId>institutiongeography-entities</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- umumi bağımlılıklar -->
<dependency>
<groupId>com.baidu.unbiz</groupId>
<artifactId>fluent-validator</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.27.Final</version>
</dependency>
</dependencies>
</project>
All maven modules’ scope is “compile”. When I debug the program, It throws different exceptions in different debug sessions. The exceptions that thrown is as follows:
java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
java.lang.NoClassDefFoundError: org.hibernate.jpa.HibernatePersistenceProvider
I presume that hibernate does not load at runtime. How can I resolve the issue? Thanks in advance.
Solution
The problem is the conflict of two different dependencies in pom.xml. Dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.27.Final</version>
</dependency>
is both defined in institutiongeography-core's pom file and sampleZkProject's pom file. When I remove the dependency from sampleZkProject's pom file, problem has been resolved. You can observe the dependency dependency in dependency hierarchy section of the eclipse.
maven clean command is advised to issue after pom arrangement.
Answered By - tahasozgen
Answer Checked By - Senaida (JavaFixing Volunteer)