拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 java.lang.NoClassDefFoundError例外:初始化期间的休眠类

java.lang.NoClassDefFoundError例外:初始化期间的休眠类

白鹭 - 2022-02-16 2095 0 0

我有一个我无法解决的问题。我需要你的帮助来解决问题。我有两个项目,“sampleZkProject”和“institutiongeography”。两者都是 Maven 项目。它们在 Java 8 上运行。

“sampleZkProject”是一个从 web 层负责的示例 zk 项目。它需要与制度地理学相互作用。机构地理是一个pom型别的maven项目。Institutionalgeography 使用hibernate 作为ORM 框架。机构地理学已经通过了测验,因此我们可以通过置信区间知道它运行良好。

“sampleZkProject”的pom结构如下:

<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 有一个类负责与机构地理互动。这个类的结构如下:

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*/

}

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:

I presume that hibernate does not load at runtime. How can I resolve the issue? Thanks in advance.

uj5u.com热心网友回复:

问题是 pom.xml 中两个不同依赖项的冲突。依赖

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.4.27.Final</version>
</dependency>

都在机构地理核心的 pom 档案和sampleZkProject的 pom 档案中定义。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *