gpt4 book ai didi

java - Hibernate 多个异常

转载 作者:行者123 更新时间:2023-12-01 10:39:59 26 4
gpt4 key购买 nike

在我提出问题之前,我知道有很多问题都有类似的问题,但它们都是旧版本,而这个新版本与它们完全不同。我主要看到的是 Hibernate 3 的问题和答案。我目前正在使用 Hibernate 5。

我在我的项目中设置了 Hibernate,我的 IDE 是 NetBeans。我已将 Hibernate 5 JAR 导入到我的项目 lib/Hibernate 文件夹中,该文件夹是 Hibernate required 文件夹中的所有 JAR。

在我的项目源代码的根文件夹(src/)上,我创建了一个名为 hibeernate.cfg.xml 的文件,其内容为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--
Program developed by Hassan Althaf.
Copyright © 2015, Hassan Althaf.
Website: http://hassanalthaf.com
-->

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connecton.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/telemart
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
correctpasswordhere
</property>
<mapping resource="com/hassanalthaf/telemart/users/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我相信这些详细信息是正确的,并且 User.hbm.xml 内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate MAPPING DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Program developed by Hassan Althaf.
Copyright © 2015, Hassan Althaf.
Website: http://hassanalthaf.com
-->

<hibernate-mapping>
<class name="User" table="users">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<property name="nicNumber" column="nic_number" type="string" />
<property name="username" column="username" type="string" />
<property name="password" column="password" type="string" />
<property name="fullName" column="full_name" type="string" />
<property name="contactNumber" column="contact_number" type="int" />
<property name="email" column="email" type="string" />
<property name="address" column="address" type="string" />
<property name="salary" column="salary" type="double" />
<property name="rank" column="rank" type="int" />
</class>
</hibernate-mapping>

现在,执行Hibernate代码的地方是Main.java文件。它实际上是一个 JavaFX 应用程序,但出于测试目的,我只是在 start 方法上尝试一下。

这就是 Main.java 现在的样子:

/*
* Program developed by Hassan Althaf.
* Copyright © 2015, Hassan Althaf.
* Website: http://hassanalthaf.com
*/
package com.hassanalthaf.telemart;

import com.hassanalthaf.telemart.users.User;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

/**
*
* @author hassan
*/
public class Main extends Application {

public static final String APPLICATION_TITLE = "TeleMart - ERP System";

@Override
public void start(Stage stage) throws Exception {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
final SessionFactory sessionFactory;

try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();

User user = new User("A", "A", "A", "A", 1, "A", "A", 1);

try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
} catch (Exception exception) {
exception.printStackTrace();
}
} catch (Exception exception) {
StandardServiceRegistryBuilder.destroy(registry);
exception.printStackTrace();
}

Parent root = FXMLLoader.load(getClass().getResource("views/MainView.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);
stage.setResizable(false);
stage.setTitle(Main.APPLICATION_TITLE);
stage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

最后,我得到的输出:

Executing /Users/hassan/NetBeansProjects/TeleMart/TeleMart/dist/run529887580/TeleMart.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1586270964.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.jboss.logging.Logger$1.run(Logger.java:2554)
at java.security.AccessController.doPrivileged(Native Method)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
at org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.<clinit>(ClassLoaderServiceImpl.java:40)
at org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:207)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:58)
at com.hassanalthaf.telemart.Main.start(Main.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$133/598547425.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/153653436.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 22 more
Exception running application com.hassanalthaf.telemart.Main

最佳答案

堆栈跟踪显示您的类路径中不存在以下类

Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException

我的这个类是在内部引用的,甚至不是由您的代码引用的。那么你可以检查你的类路径中指定的类吗?

解决方案:通过以下任一方式将所需的 jar 文件添加到类路径中。

如果您的项目是基于 Maven 的,则所需的 Maven 依赖项是:

<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

或者here 下载如果你没有使用maven项目则直接

关于java - Hibernate 多个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com