hibernate + maven + mysql 服务器配置问题

Posted

技术标签:

【中文标题】hibernate + maven + mysql 服务器配置问题【英文标题】:Problem with hibernate + maven + mysql server configuration 【发布时间】:2021-11-20 18:55:32 【问题描述】:

我在连接到我在谷歌云上创建的 mysql 数据库时遇到问题。我很容易将 mysql 工作台连接到数据库,但是在我的 maven 项目中配置休眠时遇到了问题。这是我想运行我的应用程序时显示的错误日志:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:468)
    at org.jboss.logging@3.4.2.Final/org.jboss.logging.Logger.doGetMessageLogger(Logger.java:2562)
    at org.jboss.logging@3.4.2.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2530)
    at org.jboss.logging@3.4.2.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
    at org.hibernate.orm.core@5.5.7.Final/org.hibernate.internal.HEMLogging.messageLogger(HEMLogging.java:28)
    at org.hibernate.orm.core@5.5.7.Final/org.hibernate.internal.HEMLogging.messageLogger(HEMLogging.java:24)
    at org.hibernate.orm.core@5.5.7.Final/org.hibernate.jpa.boot.internal.PersistenceXmlParser.<clinit>(PersistenceXmlParser.java:54)
    at org.hibernate.orm.core@5.5.7.Final/org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:81)
    at org.hibernate.orm.core@5.5.7.Final/org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:61)
    at org.hibernate.orm.core@5.5.7.Final/org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:50)
    at java.persistence@2.2/javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at java.persistence@2.2/javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at SlidingPuzzle/controllers.Main.start(Main.java:36)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 23 more
Exception running application controllers.Main

这是我的 pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>controllers</groupId>
    <artifactId>SlidingPuzzle</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <javafx.version>13</javafx.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>$javafx.version</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>$javafx.version</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

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

    </dependencies>

    
</project>

我的模块信息文件:

module SlidingPuzzle 
    requires javafx.controls;
    requires javafx.fxml;
    requires java.persistence;
    opens controllers to javafx.fxml;
    exports controllers;

以及我尝试初始化 EntityManger 和 EntityManagerFactory 的主文件:

package controllers;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import models.database.User;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main extends Application 

    private static Stage stage;

    public static Stage getStage()
        return stage;
    

    public static void setStage(Stage stage)
        Main.stage = stage;
    

    @Override
    public void start(Stage primaryStage) throws Exception
        setStage(primaryStage);
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(this.getClass().getResource("/views/main.fxml"));
        AnchorPane anchorPane = loader.load();

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("manager1");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction=entityManager.getTransaction();

        Scene scene = new Scene(anchorPane,1024,600);
        primaryStage.setScene(scene);
        primaryStage.setMaximized(true);
        primaryStage.setTitle("Sliding Puzzle");
        primaryStage.setMinHeight(600);
        primaryStage.setMinWidth(1024);
        primaryStage.setResizable(true);
        primaryStage.show();
    


    public static void main(String[] args) 
        launch(args);
    


我知道该错误导致我尝试创建 EntityMAnagerFactroy 但我不知道为什么会向我展示。感谢您提供任何帮助或推动正确的方向。

【问题讨论】:

也许你需要在module-info.java文件中的requires java.sql; @jewelsea 遗憾的是,它不起作用,但错误消息更改为“没有名为 manager1 的 EntityManager 的持久性提供程序”。不过还是谢谢。 好的,最后一个错误是由persistence.xml文件名中的愚蠢拼写错误引起的。但我现在得到的新错误是:“net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver”。 好的,我通过添加“需要 net.bytebuddy;”解决了这个错误到 module-info.java。我得到的下一个错误是:“引起:java.lang.NoClassDefFoundError:com/fasterxml/classmate/TypeResolver”。我希望我快结束了。 您的堆栈跟踪问题是您没有列出所有必需的模块。继续更新您的 module-info.java 以包含所需的模块。您可能需要修复其他不相关的错误(例如前面提到的持久性配置)才能使您的项目正常工作,但是 *** 问题通常仅限于一个单一的、有针对性的问答主题,因此可以充分发挥您的潜力这里的问题超出了 *** 答案的范围。 【参考方案1】:

您需要module-info.java 文件中的requires java.sql; 才能使用java.sql 模块的功能。

有关详细信息,请参阅java 9 module tutorial。

您可能需要将其他必需的模块添加到您的模块信息规范中才能使您的应用程序正常工作。

【讨论】:

以上是关于hibernate + maven + mysql 服务器配置问题的主要内容,如果未能解决你的问题,请参考以下文章

JPA、MySQL、Hibernate 和 Maven 骨架

无法设置 Hibernate + Spring + mysql + maven

hibernate + maven + mysql 服务器配置问题

spring 3、hibernate 3、maven和mysql的集成

maven+Hibernate+mysql环境搭建

无法使用 maven 项目配置 mysql hibernate