JavaFX不会连接到MYSQL [重复]
Posted
技术标签:
【中文标题】JavaFX不会连接到MYSQL [重复]【英文标题】:JavaFX won't connect to MYSQL [duplicate] 【发布时间】:2021-04-02 03:33:27 【问题描述】:我有数据存储在 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
使用 javafx 的类: 应用类:
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();
PrimaryController 类:
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);
连接mysql服务器的类:
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());
pom.xml 文件:
<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>
最后是 /recourses 中的 hibernate.properties 文件:
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】:查看异常的痕迹,我发现了这一点:
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
看看这个话题: How to solve InaccessibleObjectException (“Unable to make member accessible: module A does not 'opens package' to B”) on Java 9?
【讨论】:
以上是关于JavaFX不会连接到MYSQL [重复]的主要内容,如果未能解决你的问题,请参考以下文章