java - 使用 Hibernate 和 Jersey 连接到 MySql 数据库

标签 java mysql hibernate jersey

我第一次尝试使用 Jersey,当尝试在本地计算机上运行 Jersey 服务器时出现此异常:

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

这是我的 hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
        <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>
        <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property>
        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url"> jdbc:mysql://localhost/mydb </property>
        <property name="hibernate.connection.username"> root </property>
        <property name="hibernate.connection.password"> </property>
        <!-- List of XML mapping files -->
        <mapping class="annotations.Person"></mapping>       
    </session-factory>
</hibernate-configuration>

Jersey 的 config.yml 包含这部分:

# Database settings.
database:
    # the name of the JDBC driver, mysql in our case
    driverClass: com.mysql.jdbc.Driver
    # the username
    user: root
    # the password
    password: 
    # the JDBC URL; the database is called DWGettingStarted
    url: jdbc:mysql://localhost:3306/mydb

如何修复此异常?

最佳答案

config.yml 文件似乎缺少一些信息。

添加以下内容修复了此问题:

# Database settings.
database:
    # the name of the JDBC driver, mysql in our case
    driverClass: com.mysql.jdbc.Driver
    # the username
    user: root
    # the password
    password: 
    # the JDBC URL; the database is called DWGettingStarted
    url: jdbc:mysql://localhost:3306/mydb
    # any properties specific to your JDBC driver:
    properties:
      charSet: UTF-8
      hibernate.dialect: org.hibernate.dialect.MySQLDialect

      # the maximum amount of time to wait on an empty pool before throwing an exception
      maxWaitForConnection: 1s

      # the SQL query to run when validating a connection's liveness
      validationQuery: "/* MyApplication Health Check */ SELECT 1"

      # the minimum number of connections to keep open
      minSize: 8

      # the maximum number of connections to keep open
      maxSize: 32

      # whether or not idle connections should be validated
      checkConnectionWhileIdle: false

关于java - 使用 Hibernate 和 Jersey 连接到 MySql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38950993/

相关文章:

java - 如何更好地跟踪 Java 中的索引(相对于 Python)

JAVA整数问题

sql - 使用 SQL 查找订单数量超过 X 的客户总数

java - 将 hibernate 数据类型映射到混合类型存储过程的输出

java - 如何从数据库访问所有用户?

java - 如何用 | 分割字符串但不与\\|

Java程序连续发送大量邮件

mysql - 在 MySQL 数据库中存储端口号的最佳方式是什么?

java - Hibernate 自定义连接

java - 使用 hibernate 从数据库访问值时出现异常