我正确配置Hibernate hibernate.cfg.xml文件吗?

3 java configuration hibernate hibernate.cfg.xml

我是第一次在项目中使用Hibernate(4.2.3).我试图让它连接到H2嵌入式(本地)数据库,并拥有h2-1.3.173.jar类路径以及所有Hibernate 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
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

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

  1. 配置的SessionFactory:null
  2. 使用Hibernate内置连接池(不供生产使用!)
  3. 属性hibernate.connection.driver_class没有指定JDBC Driver类
  4. 自动提交模式:false

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

提前致谢!

Dav*_*erg 9

1)null只是会话工厂的名称.命名它没有必要.

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

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

4)Autocommit false表示您需要手动提交事务.这是正常模式.