Reputation: 1532
Before I ask the question, I know there are many questions around with a similar issue, but they are old versions, and this new version is nothing like them. I mostly see Hibernate 3 questions and answers all over. I am currently using Hibernate 5.
I have setup Hibernate in my project, and my IDE is NetBeans. I have imported the Hibernate 5 JARS into my project lib/Hibernate
folder, which is all the JARS in the required
folder of Hibernate.
On the root folder for the source codes of my project, which is src/
, I created a file called hibeernate.cfg.xml
and the contents are:
<?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>
I believe those details are correct, and, the User.hbm.xml contents:
<?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>
Now, the place where the Hibernate code is executed is the Main.java file. It is actually a JavaFX application, but for testing purposes, I am just trying it out on the start
method.
This is what Main.java is like right now:
/*
* 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);
}
}
And finally, the output I get:
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
Upvotes: 0
Views: 259
Reputation: 3433
The stack trace says following class not existing in your classpath
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
My be this class is referenced internally, even not by your code. So can you check the specified class in your classpath.
Solution: add the required jar file to your classpath by any of the following ways.
If your project is maven based, the required maven dependency is:
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
Or download it from here directly if you are not using maven project
Upvotes: 1