-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Using properties file in hibernate.cfg.xml
PostPosted: Mon Dec 15, 2008 4:38 am 
Newbie

Joined: Mon Dec 15, 2008 3:57 am
Posts: 11
How do i read connection.url, connection.username, connection.password
properties of hibernate.cfg.xml from properties file[/quote][/b]


Top
 Profile  
 
 Post subject: Re: Using properties file in hibernate.cfg.xml
PostPosted: Mon Dec 15, 2008 4:46 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
pvmk wrote:
How do i read connection.url, connection.username, connection.password
properties of hibernate.cfg.xml from properties file
[/b][/quote]


By default, Hibernate will read hibernate.properties in classpath .


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:09 am 
Newbie

Joined: Mon Dec 15, 2008 3:57 am
Posts: 11
<property name="connection.url">$url</property>
<property name="connection.username">$username</property>
<property name="connection.password">$password</property>

how do i read above hibernate.config.xml file properties from any properties file


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:22 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
pvmk wrote:
<property name="connection.url">$url</property>
<property name="connection.username">$username</property>
<property name="connection.password">$password</property>

how do i read above hibernate.config.xml file properties from any properties file


Code:
hibernate.connection.url=....
hibernate.connection.username=....
hibernate.connection.password=....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:45 am 
Newbie

Joined: Mon Dec 15, 2008 3:57 am
Posts: 11
shell i use both hibernate.properties and hibernate-config.xml for creating
org.hibernate.cfg.Configuration.
like i would like to use below properties from hibernate.properties
hibernate.connection.url=....
hibernate.connection.username=....
hibernate.connection.password=....


and remaining properties from hibernate-config.xml


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:50 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
pvmk wrote:
shell i use both hibernate.properties and hibernate-config.xml for creating
org.hibernate.cfg.Configuration.
like i would like to use below properties from hibernate.properties
hibernate.connection.url=....
hibernate.connection.username=....
hibernate.connection.password=....


and remaining properties from hibernate-config.xml


Yeah.....in our projects, usually we define the mapping in hibernate.cfg.xml, n others configuration, we put in hibernate.properties


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:53 am 
Newbie

Joined: Mon Dec 15, 2008 3:57 am
Posts: 11
SIau_Tie wrote:
pvmk wrote:
shell i use both hibernate.properties and hibernate-config.xml for creating
org.hibernate.cfg.Configuration.
like i would like to use below properties from hibernate.properties
hibernate.connection.url=....
hibernate.connection.username=....
hibernate.connection.password=....


and remaining properties from hibernate-config.xml


Yeah.....in our projects, usually we define the mapping in hibernate.cfg.xml, n others configuration, we put in hibernate.properties


In that case how i will create org.hibernate.cfg.Configuration, how do i load hibernate.properties and hibernate-config.xml files. any sample code pls


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:56 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
pvmk wrote:
SIau_Tie wrote:
pvmk wrote:
shell i use both hibernate.properties and hibernate-config.xml for creating
org.hibernate.cfg.Configuration.
like i would like to use below properties from hibernate.properties
hibernate.connection.url=....
hibernate.connection.username=....
hibernate.connection.password=....


and remaining properties from hibernate-config.xml


Yeah.....in our projects, usually we define the mapping in hibernate.cfg.xml, n others configuration, we put in hibernate.properties


In that case how i will create org.hibernate.cfg.Configuration, how do i load hibernate.properties and hibernate-config.xml files. any sample code pls



You just call :
Code:
Configuration conf = new Configuration().configure();


When new Configuration() invoked, it will try to read the configuration from hibernate.properties. When configure() invoke, it will read hibernate.cfg.xml by default, or you can also call configure(String resourceXML) instead


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 5:59 am 
Newbie

Joined: Mon Dec 15, 2008 3:57 am
Posts: 11
I my requirement i have to place hibernate.properties file out side class path, in that case how do i load hibernate.properties file.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 6:03 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
pvmk wrote:
SIau_Tie wrote:
pvmk wrote:
shell i use both hibernate.properties and hibernate-config.xml for creating
org.hibernate.cfg.Configuration.
like i would like to use below properties from hibernate.properties
hibernate.connection.url=....
hibernate.connection.username=....
hibernate.connection.password=....


and remaining properties from hibernate-config.xml


Yeah.....in our projects, usually we define the mapping in hibernate.cfg.xml, n others configuration, we put in hibernate.properties


In that case how i will create org.hibernate.cfg.Configuration, how do i load hibernate.properties and hibernate-config.xml files. any sample code pls



You just call :
Code:
Configuration conf = new Configuration().configure();


When new Configuration() invoked, it will try to read the configuration from hibernate.properties. When configure() invoke, it will read hibernate.cfg.xml by default, or you can also call configure(String resourceXML) instead


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 6:20 am 
Newbie

Joined: Mon Dec 15, 2008 3:57 am
Posts: 11
I have used below hibernate.config.xml and hibernate.properties files

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect"> org.hibernate.dialect.MySQLDialect </property>

<property name="connection.driver_class"> com.mysql.jdbc.Driver </property>
<property name="myeclipse.connection.profile"> com.mysql.jdbc.Driver </property>
<property name="hibernate.connection.release_mode">after_statement</property>
<!--<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.jdbc.batch_versioned_data">on</property>
<property name="current_session_context_class">thread</property>
-->

<property name="hibernate.c3p0.acquire_increment">3</property>
<property name="hibernate.c3p0.idle_test_period">14400</property>
<property name="hibernate.c3p0.timeout">25200</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_statements">0</property>

<property name="hibernate.c3p0.preferredTestQuery">select 1;</property>

<!-- <property name="c3p0.timeout">25200</property>
<property name="c3p0.acquireIncrement">2</property>
<property name="c3p0.automaticTestTable">C3P0TestTable</property>
<property name="c3p0.idleConnectionTestPeriod">14400</property>
<property name="c3p0.initialPoolSize">5</property>
<property name="c3p0.maxPoolSize">30</property>
<property name="c3p0.maxIdleTime">5</property>
<property name="c3p0.maxStatements">0</property>
<property name="c3p0.minPoolSize">5</property>-->


<mapping resource="hibernate/SystemMessages.hbm.xml" />
<mapping
</session-factory>
</hibernate-configuration>


hibernate properties file

connection.url=jdbc:mysql://172.16.105.172/prod_config
connection.username=link
connection.password=sh

used below code to create sessionFactory.
configuration.configure("/hibernate.cfg.xml");
configuration.configure(/hibernate.properties);
sessionFactory = configuration.buildSessionFactory();

i have got below exception
%%%% Error Creating SessionFactory %%%%
org.hibernate.HibernateException: Could not parse configuration: /hibernate.properties
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1494)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at com.conductor.analytics.uiapi.factory.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:41)
at com.conductor.analytics.uiapi.factory.BaseHibernateDAO.getSession(BaseHibernateDAO.java:35)
at com.conductor.analytics.uiapi.dao.impl.GlobalConfigDAOImpl.findById(GlobalConfigDAOImpl.java:92)
at com.conductor.analytics.uiapi.util.PropertiesResource.getGlobalConfigValueByKey(PropertiesResource.java:51)
at com.conductor.analytics.uiapi.scheduler.QuartzSchedulerServlet.init(QuartzSchedulerServlet.java:42)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3956)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4230)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:831)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:720)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1484)
... 32 more
%%%% Error Creating SessionFactory %%%%


Top
 Profile  
 
 Post subject: Re: Using properties file in hibernate.cfg.xml
PostPosted: Mon Dec 10, 2012 12:47 am 
Newbie

Joined: Mon Dec 10, 2012 12:36 am
Posts: 1
I use this for using both hibernate.cfg.xml and a properties file
java.util.Properties properties = new Properties();
properties.load(new FileInputStream("myFile.properties"));
Configuration cfg = new AnnotationConfiguration().configure();//this line reads hibernate.cfg.xml (for mapping classes)
cfg.addProperties(properties);//this one reads properties file (for connections settings)
sessionFactory = cfg.buildSessionFactory();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.