当前位置: 首页 > 知识库问答 >
问题:

JavaFX无法连接到MYSQL[重复]

谭越
2023-03-14

我有数据存储在mysql数据库,我想把它拉到我的程序,这是使用javafx,但它不会工作。如果我用一些不使用javafx的基本类提取数据,它工作得很好。但是,使用javafx,我会得到以下错误消息。我该怎么做才能解决这个问题?

org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:112)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:181)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:299)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at com.example.testing/com.example.testing.AppT.getSessionFactory(AppT.java:28)
at com.example.testing/com.example.testing.AppT.pass_args(AppT.java:42)
at com.example.testing/com.example.testing.Testing.main(Testing.java:11)
at com.example.testing/com.example.testing.PrimaryController.show_data(PrimaryController.java:13)
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 com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
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.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
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)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field int 
com.example.testing.Item.id accessible: module com.example.testing does not "opens 
com.example.testing" to module org.hibernate.orm.core
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:301)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:177)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:171)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.internal.util.ReflectHelper.ensureAccessibility(ReflectHelper.java:390)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.internal.util.ReflectHelper.findField(ReflectHelper.java:380)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.property.access.internal.PropertyAccessFieldImpl.<init>(PropertyAccessFieldImpl.java:34)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.property.access.internal.PropertyAccessStrategyFieldImpl.buildPropertyAccess(PropertyAccessStrategyFieldImpl.java:26)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:330)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:64)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:136)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:601)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:125)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.hibernate.orm.core@5.4.12.Final/org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
... 67 more
package com.example.testing;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
* JavaFX App
*/
public class App extends Application {

private static Scene scene;

@Override
public void start(Stage stage) throws IOException {
    scene = new Scene(loadFXML("primary"), 85, 40);
    stage.setScene(scene);
    stage.show();
}

static void setRoot(String fxml) throws IOException {
    scene.setRoot(loadFXML(fxml));
}

private static Parent loadFXML(String fxml) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
    return fxmlLoader.load();
}

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

}
package com.example.testing;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class PrimaryController {

@FXML // fx:id="btn"
private Button btn; // Value injected by FXMLLoader

@FXML
void show_data(ActionEvent event) {
   // I get the error here when I press the button.
   // Testing is a basic java program with one method (main) which pulls the data from server,
   // prints it to the screen, and returns it as a list.
   // It works fine without javafx.
    Testing.main(null);
}

}
package com.example.testing;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class AppT 
{
public static List<Item> myList = new ArrayList<>();
private static Session session;
private static SessionFactory getSessionFactory() throws HibernateException {
Configuration configuration =new Configuration();
configuration.addAnnotatedClass(Item.class);

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties())
        .build();

return configuration.buildSessionFactory(serviceRegistry);
}

 private static List<Item> getAll() throws Exception {
     CriteriaBuilder builder = session.getCriteriaBuilder();
     CriteriaQuery<Item> query = builder.createQuery(Item.class);
     query.from(Item.class);
     List<Item> data = session.createQuery(query).getResultList();
     return data;
 }
 
 public static List<Item> pass_args() {
        try {
            
            SessionFactory sessionFactory = getSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();
            
            //initializeData();
            myList = getAll();
            System.out.println(myList);
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
            
            e.printStackTrace();
        } finally {
            if (session != null)
            {
                session.close();
                session.getSessionFactory().close();
            }
        }
        return myList;
    }
     
 

public static void main( String[] args )
{
    // works fine without javafx
    List<Item> list = new ArrayList<>();
    list = pass_args();
    for(Item i: list) {
        System.out.println("Title= "+i.getTitle()+" "+"Type= "+i.getType()+" "+"Price= "+i.getPrice()+" "+"ID= "+i.getId()); 
    }
        
}
}
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>13</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.12.Final</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.4</version>
            <configuration>
                <mainClass>com.example.testing.App</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <id>testing</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.example.testing.Testing</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver
hibernate.show-sql = true
hibernate.hbm2ddl.auto = update
hibernate.connection.url = jdbc:mysql://127.0.0.1/items? 
useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
hibernate.connection.username = root
hibernate.connection.password = *******

很抱歉这个问题太长了。

共有1个答案

谢志用
2023-03-14

查看异常的跟踪,我检测到:

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field int 
com.example.testing.Item.id accessible: module com.example.testing does not "opens 
com.example.testing" to module org.hibernate.orm.core

请看这个主题:如何在Java9上解决InaccessibleObjectException(“无法使{member}可访问:模块{a}不‘打开{package}’到{B}”)?

 类似资料:
  • 我正在尝试连接到MySQL服务器,但出现无法处理的错误。 java.sql.SQLNonTransientConnectionException:无法创建到数据库服务器的连接。尝试重新连接3次。放弃。com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)com.mysql.cj.jdbc.excepti

  • 我最近做了一个项目,涉及使用java中的Swing在线连接到mysql数据库。然后我决定将项目转换为javaFX并尝试复制代码以连接到mysql数据库。 这是我的代码: 这是FXML文件的代码 然而,当我点击运行时,它就崩溃了,我得到错误“JavaSE二进制平台已经停止工作”。它在我以前使用摆动的应用程序中工作得很好。切换到javafx时,我所做的唯一改变是我使用了jdk 1.8而不是jdk 11

  • 我试图连接到一个名为的数据库。所有凭据都位于PHP文件名中,格式为 我正试图用这个连接到数据库 我得到这个错误: 注意:第6行的未定义变量:DB_HOST in/home/content/06/8274306/html/beta/mysuperscript.php 注意:未定义变量:第6行 /home/content/06/8274306/html/beta/mysuperscript.phpDB

  • 我正在尝试将MySQL数据库连接到XAMPP。每当我尝试在XAMPP上按start MySQL时,它都不会运行,我会得到一个错误: 这是因为我的病毒防护软件或VPN吗?我将如何更改MySQL和控制面板中的端口?如果有人能给我一些建议或指导我回答与此类似的已回答问题,将不胜感激。谢谢你。

  • 这是我使用Spring Boot的第一次练习,这是我的应用程序。属性: 这些是我的课程: > DemomysqlApplication.java @ComponentScan(BasePackages={“Com.JoyDeep.SpringBoot”})公共类DemoMysqlApplication{ noteRepository.java(接口): 从控制台,我没有看到错误。Tomcat正常启

  • 我正在尝试使用mysql数据库启动并运行一个docker容器。我有一些额外的要求。在my.cnf中有一些属性需要更改,并且有一些包含数据的sql文件应该执行,以便启动后的docker容器拥有所有可用的数据。 这应该在构建映像的过程中完成,这样容器在启动后就可以快速准备好了,不必首先插入所有的数据(这是一个相当大的量)。 所以我的计划是,从mysql-server创建一个dockerfile并运行s