java - hibernate :未设置方言。设置属性 hibernate.dialect

标签 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

知道我做错了什么吗?

最佳答案

getDialect() 的 javadoc 说:

Get an instance of the dialect specified by the current System properties.

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

使用 SessionFactoryImplementor#getDialect()

关于java - hibernate :未设置方言。设置属性 hibernate.dialect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3138061/

相关文章:

java - 一旦外键是主键的一部分,如何使用复合键映射自连接

java - VirtualEnv Python 在 MacOS 中作为 Java Python 解释器

hibernate - 在 Web 应用程序中引用 jpa persistence.xml 中的 jar 文件的正确路径是什么?

java - 无法在 hibernate 映射中加载数据

java - JPA id 生成通用策略

java - 对 transient 集合字段的 Hibernate 注释

java - 从 MySQL 的mediumblob 恢复后,PPT 文件损坏

java - 从 Java 应用程序执行任何 .exe 文件

Java - AES CBC 算法生成 SecretKeySpec 的不同方式

java - 在删除数据行之前检查其他表中是否使用了外键 hibernate