Java 如何使用 hibernate.properties 文件代替 hibernate.cfg.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23908606/
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
How to use hibernate.properties file instead of hibernate.cfg.xml
提问by Salman Ahmad
I am trying to connect to DB in a servlet using Hibernate.I have read that we can use either hibernate.cfg.xml or hibernate.properties file for configuration of session.For me it worked with xml. Now when I am trying to use properties instead of xml its not working. It is saying that hibernate.cfg.xmlnot found.But nowhere I mentioned to use xml file and infact I have deleted that xml file.
我正在尝试使用 Hibernate 连接到 servlet 中的数据库。我读到我们可以使用 hibernate.cfg.xml 或 hibernate.properties 文件来配置会话。对我来说,它使用 xml。现在,当我尝试使用属性而不是 xml 时,它不起作用。据说找不到hibernate.cfg.xml。但我没有提到使用 xml 文件,事实上我已经删除了该 xml 文件。
Please Help me. And Please correct me if I am doing anything wrong.
请帮我。如果我做错了什么,请纠正我。
采纳答案by JoaoFilipeClementeMartins
From what i understood from hibernate the best thing to do is to define the mapping in the hibernate.cfg.xml
file and other configurations in the hibernate.properties
.
根据我从 hibernate 中的理解,最好的办法是定义hibernate.cfg.xml
文件中的映射和hibernate.properties
.
An alternative approach to configuration is to specify a full configuration in a file named hibernate.cfg.xml
. This file can be used as a replacement for the hibernate.properties
file or, if both are present, to override properties.
另一种配置方法是在名为hibernate.cfg.xml
. 此文件可用作文件的替代品,hibernate.properties
或者如果两者都存在,则可以覆盖属性。
The hibernate.cfg.xml
is also more convenient once you have to tune the Hibernate cache. It is your choice to use either hibernate.properties or hibernate.cfg.xml
. Both are equivalent.
hibernate.cfg.xml
一旦您必须调整 Hibernate 缓存,这也更方便。您可以选择使用 hibernate.properties 或hibernate.cfg.xml
. 两者是等价的。
You can read more about this in the following link:
您可以在以下链接中阅读有关此内容的更多信息:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
回答by Steve C
If you are using a databasefrom a servletthen you should define a DataSourcein your server and point one hibernate property at that instead of defining everything via all the other hibernate properties you're probably using now.
如果您正在使用来自servlet的数据库,那么您应该在您的服务器中定义一个DataSource并指向一个 hibernate 属性,而不是通过您现在可能正在使用的所有其他 hibernate 属性定义所有内容。
This has the benefit of permitting you to define connection pooling and other connection related parameters independently of your application.
这样做的好处是允许您独立于应用程序定义连接池和其他连接相关参数。
For example, your production environment is likely to have a different database password than your test and development environments.
例如,您的生产环境可能与您的测试和开发环境具有不同的数据库密码。
回答by proS
This code will call hibernate.cfg.xml by default:
默认情况下,此代码将调用 hibernate.cfg.xml:
Configuration configuration = new Configuration().configure();
And this code will call hibernate.properties by default:
默认情况下,此代码将调用 hibernate.properties:
Configuration configuration = new Configuration();
Hope it helps.
希望能帮助到你。
回答by karepu
Remove .configure()
if you are using hibernate.properties
. Below code is
HibernateUtil
session factory implementation.
删除.configure()
,如果你正在使用hibernate.properties
。下面的代码是
HibernateUtil
会话工厂实现。
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
configuration.addAnnotatedClass(LookUpModel.class);
return configuration
.buildSessionFactory(serviceRegistry);
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException("There is issue in hibernate util");
}
}
hibernate.properites file
hibernate.properites 文件
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password = passsword
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect