www.example中出现错误。 www.example.com file while loading the Hibernate.cfg.xml file
示例输入和生成的错误堆栈。
Apr 26, 2023 12:01:25 PM com.infybank.ClientDemo main
INFO: Enter the Customer details
Enter the Customer name
Drona
Enter the Customer date of birth in MM/dd/yyyy format
03/05/2003
Enter the Customer address
Mahadevapalli
Enter the Customer phoneNo
7997776865
java.lang.NoClassDefFoundError: javax/transaction/SystemException
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:495)
at java.base/java.lang.Class.forName(Class.java:474)
at jboss.logging@3.3.0.Final/org.jboss.logging.Logger$1.run(Logger.java:2554)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at jboss.logging@3.3.0.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at jboss.logging@3.3.0.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at hibernate.core@5.2.11.Final/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
at hibernate.core@5.2.11.Final/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
at hibernate.core@5.2.11.Final/org.hibernate.cfg.Configuration.<clinit>(Configuration.java:85)
at CUSTOMER_DEMO/com.infybank.HibernateUtil.<clinit>(HibernateUtil.java:15)
at CUSTOMER_DEMO/com.infybank.CustomerDAO.addCustomer(CustomerDAO.java:11)
at CUSTOMER_DEMO/com.infybank.ClientDemo.main(ClientDemo.java:29)
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
Apr 26, 2023 12:01:44 PM com.infybank.HibernateUtil <clinit>
ERROR: Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/transaction/SystemException
Exception in thread "main" java.lang.ExceptionInInitializerError
at CUSTOMER_DEMO/com.infybank.HibernateUtil.<clinit>(HibernateUtil.java:23)
at CUSTOMER_DEMO/com.infybank.CustomerDAO.addCustomer(CustomerDAO.java:11)
at CUSTOMER_DEMO/com.infybank.ClientDemo.main(ClientDemo.java:29)
Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:495)
at java.base/java.lang.Class.forName(Class.java:474)
at jboss.logging@3.3.0.Final/org.jboss.logging.Logger$1.run(Logger.java:2554)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at jboss.logging@3.3.0.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2529)
at jboss.logging@3.3.0.Final/org.jboss.logging.Logger.getMessageLogger(Logger.java:2516)
at hibernate.core@5.2.11.Final/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:28)
at hibernate.core@5.2.11.Final/org.hibernate.internal.CoreLogging.messageLogger(CoreLogging.java:24)
at hibernate.core@5.2.11.Final/org.hibernate.cfg.Configuration.<clinit>(Configuration.java:85)
at CUSTOMER_DEMO/com.infybank.HibernateUtil.<clinit>(HibernateUtil.java:15)
... 2 more
Caused by: java.lang.ClassNotFoundException: javax.transaction.SystemException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
我使用的是eclipse-ide-2023。
Java版本:javaSE-19
我已经从hibernate-search-5中获取了库。8.0.Final-dist.
下面是我项目的目录结构。
Files and Dependencies
这些是项目文件。
Customer.java
package com.infybank;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "CUSTOMER")
@GenericGenerator(name = "gen", strategy = "increment")
public class Customer {
@Id
@GeneratedValue(generator = "gen")
@Column(name = "CUSTOMERID")
int customerId;
@Column(name = "CUSTOMERNAME")
String customerName;
@Temporal(TemporalType.DATE)
@Column(name = "DATEOFBIRTH")
Date dateOfBirth;
@Column(name = "ADDRESS")
String address;
@Column(name = "PHONENO")
Long phoneNo;
public Customer() {
super();
}
public Customer(String customerName, Date dateOfBirth, String address, Long phoneNo) {
this.customerName = customerName;
this.dateOfBirth = dateOfBirth;
this.address = address;
this.phoneNo = phoneNo;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Long getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(Long phoneNo) {
this.phoneNo = phoneNo;
}
}
CustomerDAO.java
package com.infybank;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class CustomerDAO implements ICustomer {
/* Method to create customer record */
@Override
public void addCustomer(Customer c1) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.save(c1);
tx.commit();
session.close();
}
}
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sample</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Mysql@272</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.infybank.Customer" />
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package com.infybank;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.jboss.logging.Logger;
public class HibernateUtil {
static Logger logger = Logger.getLogger(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration().configure("/CUSTOMER_DEMO/src/com/infybank/Hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties())
.build();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
ex.printStackTrace();
logger.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
module-info.java
/**
*
*/
/**
* @author Drona
*
*/
module CUSTOMER_DEMO {
requires dom4j;
requires hibernate.core;
requires hibernate.jpa;
requires jboss.logging;
}
ClientDemo.java
package com.infybank;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import org.hibernate.HibernateException;
import org.jboss.logging.Logger;
public class ClientDemo {
static Logger logger = Logger.getLogger(ClientDemo.class);
public static void main(String[] args) {
CustomerDAO customerdao = new CustomerDAO();
try {
logger.info("Enter the Customer details");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Customer name");
String name = sc.next();
System.out.println("Enter the Customer date of birth in MM/dd/yyyy format");
String datestr = sc.next();
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
Date dob = formatter.parse(datestr);
System.out.println("Enter the Customer address");
String address = sc.next();
System.out.println("Enter the Customer phoneNo");
Long phone = sc.nextLong();
Customer customer = new Customer(name, dob, address, phone);
customerdao.addCustomer(customer);
logger.info("One record created");
sc.close();
} catch (HibernateException e) {
logger.error(e);
} catch (Exception e) {
logger.error(e);
}
}
}
我试过使用不同的jar文件组合,但没有任何效果。
我已经搜索了stackoverflow也为相关的问题,但无法找到解决方案。
这是数据库中的表。
customer table
Table Info
1条答案
按热度按时间0pizxfdo1#
您缺少javax。交易:javax.transaction-api.jar最好是开始使用maven/gradle来处理依赖项,因为在修复此错误后,可能沿着新的错误。创建一个pom后。xml你可以简单地添加依赖项,像这样: