码迷,mamicode.com
首页 > 其他好文 > 详细

SessionFactory创建失败

时间:2015-11-17 16:37:47      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

java.lang.ExceptionInInitializerError
    at com.hibernate.utils.HibernateUtil.<clinit>(HibernateUtil.java:19)
    at com.hibernate.model.TeacherTest.launch(TeacherTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    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:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.HibernateException: ‘hibernate.dialect‘ must be set when no Connection avalable
    at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:107)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:138)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at com.hibernate.utils.HibernateUtil.<clinit>(HibernateUtil.java:13)
    ... 21 more

今天在用Hibernate AnnotationConfiguration往数据库中写入数据时遇到一个bug,用junit测试时总是提示上述异常,用debug跟踪时发现sessionFactory 获取不到,更别提获取Session了,因为以下的工具类是从 Annotation文档中直接copy过来,自认为应该不会有任何问题,但是问题就在这里~~~

public class HibernateUtil {

private static final SessionFactory sessionFactory;

    static {
        try {

            sessionFactory = new AnnotationConfiguration().buildSessionFactory();
//应该改为SessionFactory  sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); }
catch (Throwable ex) { // Log exception! System.out.println("SessionFactory创建失败,下面是失败的原因:"); ex.printStackTrace(); System.out.println("SessionFactory创建失败?"); throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return sessionFactory.openSession(); } }

 

测试类TeacherTest
public class TeacherTest {
@Test
public void launch(){
Teacher s = new Teacher();
s.setId(1);
s.setAge(6);
s.setName("bbbb");
s.setBirthday(new Date());
s.setZhicheng(ZhiCheng.A);
Session session =HibernateUtil.getSession();//在此报错
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
}
}

  

在Console栏中提示SessionFactory创建失败,下面是失败的原因:
org.hibernate.HibernateException: ‘hibernate.dialect‘ must be set when no Connection avalable

问题在于SessionFactory的获取上,我们以往用Configuration获取SessionFactory时是这样获取得:

SessionFactory  sessionFactory = new Configuration().configure().buildSessionFactory();

所以我们在用AnnotationConfiguration时也应该与之类似,即,而在文档中漏掉了configure(),导致我们获取不到sessionFavtory~~

这是文档中的小bug记得自己更正,希望能帮到你~~~

SessionFactory  sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

SessionFactory创建失败

标签:

原文地址:http://www.cnblogs.com/GeekRegister/p/4971773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!