Exception in thread "main" java.lang.ExceptionInInitializerError
at com.baolong.model.One2many_uni_fk_Test.beforeClass(One2many_uni_fk_Test.java:19)
at com.baolong.model.One2many_uni_fk_Test.main(One2many_uni_fk_Test.java:87)
Caused by: java.lang.NullPointerException
at org.slf4j.LoggerFactory.singleImplementationSanityCheck(LoggerFactory.java:192)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:113)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
... 2 more以下是我的Hibernate的配置文件 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> <!-- Database connection settings -->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=hibernate</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- 格式化打印的SQL语句 -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="com.baolong.model.Student"/>
<mapping class="com.baolong.model.Course"/>
<!--<mapping class="com.baolong.model.Score"/>
<mapping resource="com/baolong/model/Student.hbm.xml"/>
<mapping resource="com/baolong/model/StudentCard.hbm.xml"/>-->
</session-factory></hibernate-configuration>这是我的测试类:package com.baolong.model;import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;public class One2many_uni_fk_Test { public static SessionFactory sf;
@BeforeClass
public static void beforeClass() {
//sf = new AnnotationConfiguration().configure().buildSessionFactory();
sf = new Configuration().configure().buildSessionFactory();
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void saveData() {
Student stu1 = new Student();
Student stu2 = new Student();
/*Course course1 = new Course();
Course course2 = new Course();
Score score1 = new Score();
Score score2 = new Score();
Score score3 = new Score();*/
/*stu1.setName("baolong");
stu2.setName("lianglong");
course1.setName("English");
course2.setName("Math");
stu1.getCourses().add(course1);
stu1.getCourses().add(course2);
stu2.getCourses().add(course1);
score1.setCourse(course1);
score1.setStudent(stu1);
score1.setScore(88);
score2.setCourse(course2);
score2.setStudent(stu1);
score2.setScore(99);
score3.setCourse(course1);
score3.setStudent(stu2);
score3.setScore(77);*/
Session session = sf.getCurrentSession();
session.beginTransaction();
//session.save(course1);
//session.save(course2);
session.save(stu1);
session.save(stu2);
/*session.save(score1);
session.save(score2);
session.save(score3);*/
session.getTransaction().commit();
}
@Test
public static void getData() {
System.out.println("=====================");
}
public static void main(String[] args) {
try {
beforeClass();
} catch (Exception e) {
e.printStackTrace();
}
}
}
提示的信息指明说是因为解析Hibernate配置文件是出错,无法执行这一句
sf = new Configuration().configure().buildSessionFactory();
请大家帮我一下,我已经浪费一早上时间,没有解决掉这个问题,谢谢大家了....
at com.baolong.model.One2many_uni_fk_Test.beforeClass(One2many_uni_fk_Test.java:19)
at com.baolong.model.One2many_uni_fk_Test.main(One2many_uni_fk_Test.java:87)
Caused by: java.lang.NullPointerException
at org.slf4j.LoggerFactory.singleImplementationSanityCheck(LoggerFactory.java:192)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:113)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
... 2 more以下是我的Hibernate的配置文件 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> <!-- Database connection settings -->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=hibernate</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property> <!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- 格式化打印的SQL语句 -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="com.baolong.model.Student"/>
<mapping class="com.baolong.model.Course"/>
<!--<mapping class="com.baolong.model.Score"/>
<mapping resource="com/baolong/model/Student.hbm.xml"/>
<mapping resource="com/baolong/model/StudentCard.hbm.xml"/>-->
</session-factory></hibernate-configuration>这是我的测试类:package com.baolong.model;import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;public class One2many_uni_fk_Test { public static SessionFactory sf;
@BeforeClass
public static void beforeClass() {
//sf = new AnnotationConfiguration().configure().buildSessionFactory();
sf = new Configuration().configure().buildSessionFactory();
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
@AfterClass
public static void afterClass() {
sf.close();
}
@Test
public void saveData() {
Student stu1 = new Student();
Student stu2 = new Student();
/*Course course1 = new Course();
Course course2 = new Course();
Score score1 = new Score();
Score score2 = new Score();
Score score3 = new Score();*/
/*stu1.setName("baolong");
stu2.setName("lianglong");
course1.setName("English");
course2.setName("Math");
stu1.getCourses().add(course1);
stu1.getCourses().add(course2);
stu2.getCourses().add(course1);
score1.setCourse(course1);
score1.setStudent(stu1);
score1.setScore(88);
score2.setCourse(course2);
score2.setStudent(stu1);
score2.setScore(99);
score3.setCourse(course1);
score3.setStudent(stu2);
score3.setScore(77);*/
Session session = sf.getCurrentSession();
session.beginTransaction();
//session.save(course1);
//session.save(course2);
session.save(stu1);
session.save(stu2);
/*session.save(score1);
session.save(score2);
session.save(score3);*/
session.getTransaction().commit();
}
@Test
public static void getData() {
System.out.println("=====================");
}
public static void main(String[] args) {
try {
beforeClass();
} catch (Exception e) {
e.printStackTrace();
}
}
}
提示的信息指明说是因为解析Hibernate配置文件是出错,无法执行这一句
sf = new Configuration().configure().buildSessionFactory();
请大家帮我一下,我已经浪费一早上时间,没有解决掉这个问题,谢谢大家了....
再根据at com.baolong.model.One2many_uni_fk_Test.beforeClass(One2many_uni_fk_Test.java:19)我怀疑 是 sf 是空 不能close
这里报空指针
很可能new Configuration()是空的。
如果你的hibernate配置文件不是放在src/下的,那么你可能需要这么写
factory = new Configuration().configure(“初始化的配置文件”).buildSessionFactory();
<mapping class="com.baolong.model.Course"/>
<property name="hbm2ddl.auto">create</property>
去掉
<property name="current_session_context_class">thread</property>
加全
学这样的东西,先去官方文档看看,慢慢学
"这样eclipse出问题的时候,就会发生错误"
指的是什么问题呢,能说明白点么?