我正在练习hibernate,但是我收到了一些错误消息,我还无法解决这个问题。
控制台:
Error in creating SessionFactory object.javax/transaction/SystemException
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at screens.Screen_User.save(Screen_User.java:216)
at screens.Screen_User$1.actionPerformed(Screen_User.java:184)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
相关课程:
package screens;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import classes.Car_Rental;
import classes.User;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.Expression;
import java.io.File;
public class Screen_User extends JPanel{
private JTextField txtUsername;
private JLabel lblName;
private JLabel lblSurname;
private JTextField txtSurname;
private JLabel lblGender;
private JTextField txtGender;
private JLabel lblAddress;
private JTextField txtAddress;
private JPasswordField passwordField;
private JTextField txtName;
private JLabel lblMailAddress;
private JTextField txtMailAddress;
private JButton btnKaydet;
User user= new User();
public Screen_User(){
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JLabel lblUserName = new JLabel("Username:");
GridBagConstraints gbc_lblUserName = new GridBagConstraints();
gbc_lblUserName.anchor = GridBagConstraints.WEST;
gbc_lblUserName.insets = new Insets(0, 0, 5, 5);
gbc_lblUserName.gridx = 1;
gbc_lblUserName.gridy = 1;
add(lblUserName, gbc_lblUserName);
txtUsername = new JTextField();
txtUsername.setColumns(10);
GridBagConstraints gbc_txtUsername = new GridBagConstraints();
gbc_txtUsername.insets = new Insets(0, 0, 5, 5);
gbc_txtUsername.anchor = GridBagConstraints.WEST;
gbc_txtUsername.gridx = 2;
gbc_txtUsername.gridy = 1;
add(txtUsername, gbc_txtUsername);
JLabel lblPassword = new JLabel("Password:");
GridBagConstraints gbc_lblPassword = new GridBagConstraints();
gbc_lblPassword.anchor = GridBagConstraints.WEST;
gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
gbc_lblPassword.gridx = 1;
gbc_lblPassword.gridy = 2;
add(lblPassword, gbc_lblPassword);
passwordField = new JPasswordField();
GridBagConstraints gbc_passwordField = new GridBagConstraints();
gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
gbc_passwordField.insets = new Insets(0, 0, 5, 5);
gbc_passwordField.gridx = 2;
gbc_passwordField.gridy = 2;
add(passwordField, gbc_passwordField);
lblName = new JLabel("Name:");
GridBagConstraints gbc_lblName = new GridBagConstraints();
gbc_lblName.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblName.insets = new Insets(0, 0, 5, 5);
gbc_lblName.gridx = 1;
gbc_lblName.gridy = 3;
add(lblName, gbc_lblName);
txtName = new JTextField();
GridBagConstraints gbc_txtName = new GridBagConstraints();
gbc_txtName.anchor = GridBagConstraints.WEST;
gbc_txtName.insets = new Insets(0, 0, 5, 5);
gbc_txtName.gridx = 2;
gbc_txtName.gridy = 3;
add(txtName, gbc_txtName);
txtName.setColumns(10);
lblSurname = new JLabel("Surname:");
GridBagConstraints gbc_lblSurname = new GridBagConstraints();
gbc_lblSurname.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblSurname.insets = new Insets(0, 0, 5, 5);
gbc_lblSurname.gridx = 1;
gbc_lblSurname.gridy = 4;
add(lblSurname, gbc_lblSurname);
txtSurname = new JTextField();
GridBagConstraints gbc_txtSurname = new GridBagConstraints();
gbc_txtSurname.anchor = GridBagConstraints.WEST;
gbc_txtSurname.insets = new Insets(0, 0, 5, 5);
gbc_txtSurname.gridx = 2;
gbc_txtSurname.gridy = 4;
add(txtSurname, gbc_txtSurname);
txtSurname.setColumns(10);
lblGender = new JLabel("Gender:");
GridBagConstraints gbc_lblGender = new GridBagConstraints();
gbc_lblGender.anchor = GridBagConstraints.WEST;
gbc_lblGender.insets = new Insets(0, 0, 5, 5);
gbc_lblGender.gridx = 1;
gbc_lblGender.gridy = 5;
add(lblGender, gbc_lblGender);
txtGender = new JTextField();
GridBagConstraints gbc_txtGender = new GridBagConstraints();
gbc_txtGender.anchor = GridBagConstraints.WEST;
gbc_txtGender.insets = new Insets(0, 0, 5, 5);
gbc_txtGender.gridx = 2;
gbc_txtGender.gridy = 5;
add(txtGender, gbc_txtGender);
txtGender.setColumns(10);
lblAddress = new JLabel("Address:");
GridBagConstraints gbc_lblAddress = new GridBagConstraints();
gbc_lblAddress.anchor = GridBagConstraints.WEST;
gbc_lblAddress.insets = new Insets(0, 0, 5, 5);
gbc_lblAddress.gridx = 1;
gbc_lblAddress.gridy = 6;
add(lblAddress, gbc_lblAddress);
txtAddress = new JTextField();
GridBagConstraints gbc_txtAddress = new GridBagConstraints();
gbc_txtAddress.anchor = GridBagConstraints.WEST;
gbc_txtAddress.insets = new Insets(0, 0, 5, 5);
gbc_txtAddress.gridx = 2;
gbc_txtAddress.gridy = 6;
add(txtAddress, gbc_txtAddress);
txtAddress.setColumns(10);
lblMailAddress = new JLabel("Mail address:");
GridBagConstraints gbc_lblMailAddress = new GridBagConstraints();
gbc_lblMailAddress.anchor = GridBagConstraints.EAST;
gbc_lblMailAddress.insets = new Insets(0, 0, 5, 5);
gbc_lblMailAddress.gridx = 1;
gbc_lblMailAddress.gridy = 7;
add(lblMailAddress, gbc_lblMailAddress);
txtMailAddress = new JTextField();
GridBagConstraints gbc_txtMailAddress = new GridBagConstraints();
gbc_txtMailAddress.anchor = GridBagConstraints.NORTHWEST;
gbc_txtMailAddress.insets = new Insets(0, 0, 5, 5);
gbc_txtMailAddress.gridx = 2;
gbc_txtMailAddress.gridy = 7;
add(txtMailAddress, gbc_txtMailAddress);
txtMailAddress.setColumns(10);
btnKaydet = new JButton("Kaydet");
btnKaydet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
save();
}
});
GridBagConstraints gbc_btnKaydet = new GridBagConstraints();
gbc_btnKaydet.insets = new Insets(0, 0, 0, 5);
gbc_btnKaydet.gridx = 1;
gbc_btnKaydet.gridy = 9;
add(btnKaydet, gbc_btnKaydet);
}
public void save(){
Session sess = null;
Transaction tran = null;
try{
SessionFactory sessFact = createSessionFactory();
sess = sessFact.openSession();
tran = sess.beginTransaction();
initUser();
sess.save(user);
tran.commit();
}
catch (Throwable e) {
System.err.println("Error in creating SessionFactory object."
+ e.getMessage());
throw new ExceptionInInitializerError(e);
}
finally{
sess.close();
}
}
public void initUser(){
user.setAddress(txtAddress.toString());
user.setGender(txtGender.toString());
user.setMail_address(txtMailAddress.toString());
user.setName(txtName.toString());
user.setPassword(passwordField.toString());
user.setSurname(txtSurname.toString());
user.setUsername(txtUsername.toString());
}
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory createSessionFactory() {
// Configuration configuration = new Configuration();
//configuration.configure();
Configuration configuration = new Configuration().configure(new File("C:\\Users\\user\\workspace\\Hotel\\src\\hibernate"));
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hotel
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root123
</property>
<!-- List of XML mapping files -->
<mapping resource="Hotel.hbm.xml"/>
<mapping resource="Car_Rental.hbm.xml"/>
<mapping resource="Hotel_Reservation.xml"/>
<mapping resource="Ticket_Reservation.hbm.xml"/>
<mapping resource="Tour_Reservation.hbm.xml"/>
<mapping resource="Touristic_Place.hbm.xml"/>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我正在寻找6小时的解决方案,但我还没找到。希望我能尽快解决这个问题。谢谢。
答案 0 :(得分:0)
您的文件路径错误
Configuration configuration = new Configuration().configure(new File("C:\\Users\\user\\workspace\\Hotel\\src\\hibernate"));
应该是
Configuration configuration = new Configuration().configure(new File("C:\\Users\\user\\workspace\\Hotel\\src\\hibernate.cfg.xml"));
---------^
除此之外,您必须提供应用程序的相对路径。否则,您需要在部署时更改此路径。