Hibernat报错找不到Hibernate.util类

Anliexo 2016-12-31 08:08:14
这是我的Hibernate.util类
这是我导入的jar包, 看看还差什么吗?

package com.siggy;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static Configuration cfg=null;
private static SessionFactory factory=null;
private static Session session=null;
static {
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
}
public static Session getsession(){
if(factory!=null)
return factory.openSession();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
return factory.openSession();
}
//关闭session方法
public static void closeSession(){
if(session!=null&&session.isOpen())
session.close();
}

}

这是一个学生类
package com.siggy.pojo;
/**
* Student 学生类
* */
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

这是Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.siggy.pojo.Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="id"/>
<property name="name"/>
</class>
</hibernate-mapping>

这是hibernate.cfg.xml
<!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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!--判断是否打印sql语句 -->
<property name="show_sql">true</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<mapping resource="com/siggy/pojo/Student.hbm.xml"/>
</session-factory>

</hibernate-configuration>

然后开始写测试类
package com.siggy.test;

import java.sql.SQLException;

import javax.sql.rowset.serial.SerialException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import com.siggy.HibernateUtil;
import com.siggy.pojo.Student;

public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否生成ddl脚本 第二个参数 是否执行到数据库中
se.create(true, true);
}
@Test
public void testSave() throws HibernateException, SerialException, SQLException{
Session session = null;
Transaction tx=null;
try{
session = HibernateUtil.getsession();
tx = session.beginTransaction();
Student stu=new Student();
stu.setName("张三");

session.save(stu);

tx.commit();

}catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
throw e;
}finally{
HibernateUtil.closeSession();
}
}

}

一下是报错的结果,表是可以创建的,但是数据没有办法输入数据库
java.lang.NoClassDefFoundError: Could not initialize class com.siggy.HibernateUtil
at com.siggy.test.HibernateTest.testSave(HibernateTest.java:44)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)



有的时候是可以添加数据的,网上查了很多方法,但是并没有解决,希望能给一个比较好的解决方案
...全文
333 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
JE_GE 2017-01-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/je_ge/article/details/53235868
SmarkMeng 2017-01-03
  • 打赏
  • 举报
回复
重新编译下这个类或者整个项目clean up rebuild

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧