Java 我是否正确配置了 Hibernate hibernate.cfg.xml 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19318396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Am I configuring Hibernate hibernate.cfg.xml file correctly?
提问by
I am using Hibernate (4.2.3) in a project for the first time. I am trying to get it to connect to an H2 embedded (local) DB and have the h2-1.3.173.jar
on the classpath as well as all the Hibernate JARs. I'm getting some disturbing error messages from Hibernate in my log output that make me wonder if I am not configuring Hibernate correctly. Here is the output I'm seeing in the logs:
我第一次在项目中使用 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
And here is my hibernate.cfg.xml
file:
这是我的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>
From the log output, I am concerned about several things:
从日志输出中,我担心几件事:
- Configured SessionFactory: null
- Using Hibernate built-in connection pool (not for production use!)
- No JDBC Driver class was specified by property hibernate.connection.driver_class
- Autocommit mode: false
- 配置的会话工厂:null
- 使用 Hibernate 内置连接池(不适用于生产用途!)
- 属性 hibernate.connection.driver_class 未指定 JDBC 驱动程序类
- 自动提交模式:false
It doessee that my connection pool size is 1, but I'm worried that this is a default value that Hibernate resorts to when it can't find/parse a hibernate.cfg.xml
file. Why is my SessionFactory null? Why is Hibernate using it's own built-in connection pool? Why can't it find my JDBC Driver class when h2-1.3.173.jar
is on the class path? What is "Autocommit mode" and why is it false?
它确实看到我的连接池大小为 1,但我担心这是 Hibernate 在无法找到/解析hibernate.cfg.xml
文件时采用的默认值。为什么我的 SessionFactory 为空?为什么 Hibernate 使用它自己的内置连接池?为什么h2-1.3.173.jar
在类路径上找不到我的 JDBC Driver 类?什么是“自动提交模式”,为什么它是错误的?
Thanks in advance!
提前致谢!
采纳答案by David Weinberg
1) The null is just the name of the session factory. Naming it is not necessary.
1) null 只是会话工厂的名称。命名它是没有必要的。
2) You will want to use something like c3p0. Take a look at https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool. If you set those settings, it will turn on c3p0.
2)你会想要使用像c3p0这样的东西。看看https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool。如果您设置了这些设置,它将打开 c3p0。
3) You are setting driver.class
but it's driver_class
3)你正在设置,driver.class
但它是driver_class
4) Autocommit false means you will need to manually commit your transactions. This is the normal pattern.
4) Autocommit false 意味着您需要手动提交您的交易。这是正常的模式。