2014-12-13 94 views
1

堆栈跟踪:Hibernate配置例外

Initial SessionFactory creation failed.org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20) 
    at org.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:9) 
    at org.hibernate.tutorial.EventManager.createAndStoreEvent(EventManager.java:23) 
    at org.hibernate.tutorial.EventManager.main(EventManager.java:16) 
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) 
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) 
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845) 
    at org.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:14) 
    ... 3 more 

我的hibernate配置:

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
     <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 
     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 
     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</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.internal.NoCacheProvider</property> 
     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 
     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create</property> 
     <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

我的代码:

public class HibernateUtil { 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      return new Configuration().configure().buildSessionFactory(
       new StandardServiceRegistryBuilder().build()); 
     } 
     catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 
public class EventManager { 

    public static void main(String[] args) { 
     EventManager mgr = new EventManager(); 

     if (args[0].equals("store")) { 
      mgr.createAndStoreEvent("My Event", new Date()); 
     } 

     HibernateUtil.getSessionFactory().close(); 
    } 

    private void createAndStoreEvent(String title, Date theDate) { 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     session.beginTransaction(); 

     Event theEvent = new Event(); 
     theEvent.setTitle(title); 
     theEvent.setDate(theDate); 
     session.save(theEvent); 

     session.getTransaction().commit(); 
    } 

} 
+2

不知道它的相关的异常,但访问MySQL数据库与HSQL司机并没有太大的意义。 – 2014-12-13 10:30:57

+0

请更改您的连接驱动程序,使用com.mysql.jdbc.Driver连接到MySQL – martinusadyh 2014-12-13 13:54:37

回答

0

您的sessionfactory属性名称不正确。他们应该

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hibernate.show_sql">true</property> 
<property name="hibernate.cache.use_second_level_cache">false</property> 

注意属性名是hibernate.

+0

我的配置来自官方文档。属性名称为hibernate.cfg.xml。 – dimit 2014-12-15 02:47:32