hibernate未初始化

package com.jady.test;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.jady.domain.Dept;
import com.jady.domain.Emp;
import com.jady.domain.HibernateSessionFactory;

public class TestEmpDept {

@Test
public void test(){
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateSessionFactory.getSession();
        tx = session.beginTransaction();

        Dept dept = new Dept();
        dept.setName("财务");

        Emp e1 = new Emp();
        e1.setName("tom");
        Emp e2 = new Emp();
        e2.setName("jerry");            

//

//

// //关系
e1.setDept(dept);
e2.setDept(dept);
//

Set emps = new HashSet();
emps.add(e1);
emps.add(e2);
//

dept.setEmps(emps);
//

// //保存
// session.save(dept);
// session.save(e1);
// session.save(e2);

        tx.commit();
    } catch (HibernateException e) {
        e.printStackTrace();
        tx.rollback();
    }finally{
        HibernateSessionFactory.closeSession();
    }



}

}

HibernateSessionFactory是用MyEclipse自动添加的,运行报错如下:

java.lang.NoClassDefFoundError: Could not initialize class com.jady.domain.HibernateSessionFactory
at com.jady.test.TestEmpDept.test(TestEmpDept.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

INFO Environment:514 - Hibernate 3.2.5
INFO Environment:547 - hibernate.properties not found
INFO Environment:681 - Bytecode provider name : cglib
INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
INFO Configuration:553 - Reading mappings from resource : com/jady/domain/Emp.hbm.xml
INFO HbmBinder:300 - Mapping class: com.jady.domain.Emp -> Emp
INFO Configuration:553 - Reading mappings from resource : com/jady/domain/Dept.hbm.xml
INFO HbmBinder:300 - Mapping class: com.Jady.domain.Dept -> Dept

6个回答

我建议想这种配置文件你最好用工具来生成,如果你用的是myeclipse的话,应该带有这种工具了

HibernateSessionFactory是不是没有编译,你到bin目录的响应路径下查找时候有生成相应的class文件,如果没有,就重新编译一下

我注意到了Could not initialize class com.jady.domain.HibernateSessionFactory
也就是说HibernateSessionFactory 在初始化的时候就出问题了,也就是说可能是静态代码块出问题了,你仔细检查一下,看代码有没有问题,如果没有查一下配置文件有没有问题

哦,那可能就是配置文件的问题,你把你的配置文件发出来看看

先试试在 /hibernate.cfg.xml 的配置文件中把Emp.hbm.xml,Dept.hbm.xml去掉试试

应该是Dept 映射的时候出现异常了,按理应该会有异常信息的,不知道为什么没有,
com.Jady.domain.Dept的名字错了,应该是com.jady.domain.Dept

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问