休眠状态:未设置方言。设置属性hibernate.dialect

Mar*_*itt 5 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 -->
Run Code Online (Sandbox Code Playgroud)

我用这条线消耗:

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

然后,在其他地方我尝试这样做:

SimpleSelect pkSelect = new SimpleSelect(Dialect.getDialect());
Run Code Online (Sandbox Code Playgroud)

导致以下异常:

org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
Run Code Online (Sandbox Code Playgroud)

注意,以下摘录自日志:

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
Run Code Online (Sandbox Code Playgroud)

有什么想法我做错了吗?

Boz*_*zho 4

javadocgetDialect()说:

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

因此,您必须进行hibernate.dialect配置才能hibernate.properties使此方法发挥作用。

使用SessionFactoryImplementor#getDialect()