Hibernate:没有设置方言。设置属性hibernate.dialect

时间:2010-06-29 05:24:43

标签: java hibernate

我有以下hibernate.cfg.xml

<hibernate-configuration>
   <session-factory>
      <property name="hibernate.format_sql">true</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/EJB</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">password</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
 <!-- Snip -->

我使用此行消费:

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

然后,在其他地方我试试这个:

SimpleSelect pkSelect = new SimpleSelect(Dialect.getDialect());

导致以下异常:

org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.

注意,以下日志摘录:

02:26:48,714  INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
02:26:48,717  INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
02:26:48,909 DEBUG Configuration:1387 - hibernate.dialect=org.hibernate.dialect.MySQLDialect

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:4)

getDialect()的javadoc说:

  

获取当前系统属性指定的方言实例。

因此,您必须在hibernate.dialect中配置hibernate.properties才能使此方法有效。

使用SessionFactoryImplementor#getDialect()