java - 我是否正确配置了 Hibernate hibernate.cfg.xml 文件?

标签 java hibernate configuration hibernate.cfg.xml

我是第一次在项目中使用 Hibernate (4.2.3)。我试图让它连接到 H2 嵌入式(本地)数据库,并在类路径以及所有 Hibernate JAR 上都有 h2-1.3.173.jar。我在日志输出中收到一些来自 Hibernate 的令人不安的错误消息,这让我想知道我是否没有正确配置 Hibernate。这是我在日志中看到的输出:

604 [main] INFO org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
707 [main] INFO org.hibernate.Version - HHH000412: Hibernate Core {4.2.3.Final}
769 [main] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
771 [main] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
2192 [main] INFO org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: hibernate.cfg.xml
2192 [main] INFO org.hibernate.cfg.Configuration - HHH000040: Configuration resource: hibernate.cfg.xml
2835 [main] INFO org.hibernate.cfg.Configuration - HHH000041: Configured SessionFactory: null
3313 [main] INFO org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!)
3313 [main] WARN org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000148: No JDBC Driver class was specified by property hibernate.connection.driver_class
3313 [main] INFO org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 1
3313 [main] INFO org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000006: Autocommit mode: false

这是我的 hibernate.cfg.xml 文件:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- DataSource & Connection info. -->
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="hibernate.connection.driver.class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:file:/${MYAPP_HOME}/data/myapp_db</property>
        <property name="hibernate.connection.username">myapp</property>
        <property name="hibernate.connection.password">12345</property>
        <property name="hibernate.connection.pool_size">1</property>

        <!-- General Hibernate settings. -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <!-- DDL Mode. -->
        <property name="hbm2ddl.auto">validate</property>

        <!-- 2nd Level Cache. -->
        <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

        <!-- All our Hibernate mapping XML files. -->
        <mapping class="net.myapp.common.dto.WordDTO" />
    </session-factory>
</hibernate-configuration>

从日志输出中,我关心几件事:

  1. 已配置的 SessionFactory:null
  2. 使用 Hibernate 内置连接池(不适用于生产!)
  3. 属性 hibernate.connection.driver_class 未指定 JDBC 驱动程序类
  4. 自动提交模式:false

确实看到我的连接池大小是 1,但我担心这是 Hibernate 在找不到/解析 hibernate 时使用的默认值。 cfg.xml 文件。为什么我的 SessionFactory 为空?为什么 Hibernate 使用它自己的内置连接池?当 h2-1.3.173.jar 在类路径上时,为什么它找不到我的 JDBC Driver 类?什么是“自动提交模式”,为什么它是错误的?

提前致谢!

最佳答案

1) null 只是 session 工厂的名称。命名它是没有必要的。

2) 你会想要使用像 c3p0 这样的东西。看看https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool .如果您设置这些设置,它将打开 c3p0。

3) 您正在设置 driver.class 但它是 driver_class

4) Autocommit false 意味着您将需要手动提交您的事务。这是正常模式。

关于java - 我是否正确配置了 Hibernate hibernate.cfg.xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318396/

相关文章:

java - 如何在AspectJ中使用@After和@AfterThrowing之一?

java - JPA/hibernate : How to persist duplicate values in same session for field having @Id annotation?

java - hibernate加入了策略继承和多态

deployment - 虚拟环境中的软件部署

python - 如何解析并附加到 flexget yaml 配置文件?

Java SWT 列表 - 使其不可选择

java - 正则表达式拆分指令

java - 不属于公共(public) API 的类

java - 你如何处理业务层的 Hibernate Session?

mysql - phpMyAdmin设置了空白用户和pass,但是 'allownopassword'设置为false,可以登录吗?