-->
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.  [ 9 posts ] 
Author Message
 Post subject: Can hibernate.cfg.xml refer to another file for values
PostPosted: Fri Apr 06, 2007 10:52 am 
Newbie

Joined: Fri Apr 06, 2007 10:46 am
Posts: 4
Hi Folks

I am using hibernate with MS-SQL Server. I would like to know if I can get hibernate.cfg.xml to refer to mssql-ds.xml for the connection-url, database username and database password, instead of putting these values in the hibernate.cfg.xml file itself.

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 11:59 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can use XML placeholders (XML external DTD entities), I think there is an example in the Hibernate reference documentation for mapping files. This also works in XML configuration files.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 2:00 pm 
Newbie

Joined: Fri Apr 06, 2007 10:46 am
Posts: 4
[quote="christian"]You can use XML placeholders (XML external DTD entities), I think there is an example in the Hibernate reference documentation for mapping files. This also works in XML configuration files.[/quote]

I have your book right here and I have looked at the configuration related pages, but I am not able to find the answer.

I have two file "mssql-ds.xml" and "hibernate.cfg.xml". I do not want to put the connection-url, database username and password in both the file. Actually I want "hibernate.cfg.xml" to use the connection-url etc information from mssql-ds.xml. I tried to use the .addFile() method in Configuration class, but it won't take the data defined in mssql-ds.xml.

13:52:49,375 INFO [Configuration] Reading mappings from file: mssql-ds.xml
13:52:49,375 ERROR [XMLHelper] Error parsing XML: mssql-ds.xml(17) Document is invalid: no grammar found.

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 2:43 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I don't explain it in the book for configuration files, but I explain how it works for mapping files. Since both are XML, and this is an XML mechanism, it's the same approach.

I never thought anyone would use this for configuration files, you should consider several configuration files if you have several different configurations.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 2:46 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, it doesn't work for what you want to do. You can't include stuff in mssql-ds.xml, only in hibernate.cfg.xml, and you can't include one into the other. I don't understand why you want to even do that, the whole point of a datasource is that you configure a datasource name in mssql-ds.xml and then use that name in hibernate.cfg.xml. There is no reason why you would want to put the username and password into hibernate.cfg.xml as well, they are not used if you specify a datasource name.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 3:08 pm 
Newbie

Joined: Fri Apr 06, 2007 10:46 am
Posts: 4
[quote="christian"]Oh, it doesn't work for what you want to do. You can't include stuff in mssql-ds.xml, only in hibernate.cfg.xml, and you can't include one into the other. I don't understand why you want to even do that, the whole point of a datasource is that you configure a datasource name in mssql-ds.xml and then use that name in hibernate.cfg.xml. There is no reason why you would want to put the username and password into hibernate.cfg.xml as well, they are not used if you specify a datasource name.[/quote]

First of all, I immensely thank you for your time and answers:-

Yes, I have the connection-url, username and password defined only in mssql-ds.xml.

part of mssql-ds.xml:-

<datasources>
<local-tx-datasource>
<jndi-name>MSSQLDS</jndi-name>
<connection-url>jdbc:jtds:sqlserver://localhost:1433/eagle_eye</connection-u
rl>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<user-name>user</user-name>
<password>userpass</password>
<close tags properly>

I just want to reference that from hibernate.cfg.xml. So I did this
in hibernate.cfg.xml:-

<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="connection.datasource">java:MSSQLDS</property>

There is some problem here, since hibernate is NOT able to refer to the datasource as defined above.

Is there any syntax error??

Any help is greatly appreciated.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 3:35 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Let me guess, you are not using an application server? Datasources work in application servers. If you don't have an application server, you can't use datasources.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 3:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you have an application server, deploy your datasource, go into the console of your application server, verify that the datasource is bound under its name in the JNDI tree. Then use that JNDI name in your Hibernate configuration and deploy your archive with Hibernate stuff on the server.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 4:09 pm 
Newbie

Joined: Fri Apr 06, 2007 10:46 am
Posts: 4
Hi Christian

It works like a charm. All I had to do was set the JNDI as "java:whatever"
and it references the values set in mssql-ds.xml.

With much regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.