我无法解决这些问题,也无法运行这个休眠程序也有任何帮助。我写过两个hibernate程序,错误都是一样的。所以我发布了我的注释hibernate程序…
java模块。Base不"打开java.lang";致未命名模块@7fc2413d
异常堆栈
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @7fc2413d
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
Customer.java
@Entity
@Table(name="customers")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="cid")
private int cid;//P.K
@Column(name="cname")
private String cname;
@Column(name="email")
private String email;
@Column(name="phone")
private long phone;
@Column(name="city")
private String city;
@Column(name="bal")
private double bal;
// constructor
// getter setter
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jtcindiadb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Pr3000</property>
<property name="hibernate.connection.pool_size">99</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.jtcindia.hibernate.Customer"></mapping>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package com.jtcindia.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
static SessionFactory factory;
static {
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg=(AnnotationConfiguration)cfg.configure("hibernate.cfg.xml");
factory=cfg.buildSessionFactory();
}
public static SessionFactory setSessionFactory(){
return factory;
}
}
Client1.java
package com.jtcindia.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class Client1 {
public static void main(String[] args) {
Transaction tx=null;
try {
SessionFactory sf=HibernateUtil.setSessionFactory();
Session session=sf.openSession();
tx=session.beginTransaction();
Customer cust=new Customer("som","som@jtc",123,"Noida",25000.00);
session.save(cust);
tx.commit();
session.close();
System.out.println("record inserted");
}catch(Exception e) {
e.printStackTrace();
if(tx!=null)tx.rollback();
}
}
}
您不应该在尚未测试过的新Java版本上使用旧版本的Hibernate。更新Hibernate版本或使用旧的Java版本。