-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can Hibernate mapping files be located outside the classpath
PostPosted: Thu Nov 30, 2006 1:19 pm 
Newbie

Joined: Mon Sep 25, 2006 7:26 am
Posts: 1
Our client asked us to move all Hibernate mapping and config files outside our WAR file. We did that, but included this external location in the application server (WebSphere) classpath.

Now, the client wants us to remove the classpath reference and explore file-loader/class-loader options to load the mapping files when a Hibernate request is made.

My question: Can teh Hibernate config and mapping files be located outside the application, not in the classpath, and still be referenced?

We are using Hibernate 3.0.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 1:46 pm 
Newbie

Joined: Mon Jan 05, 2004 11:04 am
Posts: 16
Hi,

you need the classpath ;-((

The new Configuration() needs the classpath.

Here is a part of the new hibernate book (chapter 2 is avalaible
at http://www.manning.com/bauer2/chapter2.pdf) which describes this.



Building a SessionFactory
This is an example of a typical Hibernate startup procedure, in one line of code,
using automatic configuration file detection:
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Wait—how did Hibernate know where the configuration file was located and
which one to load?
When new Configuration() is called, Hibernate searches for a file named
hibernate.properties in the root of the classpath. If it’s found, all hibernate.*
properties are loaded and added to the Configuration object.
When configure() is called, Hibernate searches for a file named hibernate.
cfg.xml in the root of the classpath, and an exception is thrown if it can’t
be found. You don’t have to call this method if you don’t have this configuration
file, of course. If settings in the XML configuration file are duplicates of properties
set earlier, the XML settings override the previous ones.
The location of the hibernate.properties configuration file is always the
root of the classpath
, outside of any package. If you wish to use a different file or
to have Hibernate look in a subdirectory of your classpath for the XML configuration
file, you must pass a path as an argument of the configure() method:
SessionFactory sessionFactory = new Configuration()
.configure("/persistence/auction.cfg.xml")
.buildSessionFactory();
Finally, you can always set additional configuration options or mapping file locations
on the Configuration object programmatically, before building the SessionFactory:
SessionFactory sessionFactory = new Configuration()
.configure("/persistence/auction.cfg.xml")
.setProperty(Environment.DEFAULT_SCHEMA, "CAVEATEMPTOR")
.addResource("auction/CreditCard.hbm.xml")
.buildSessionFactory();
Many sources for the configuration are applied here: First the hibernate.properties
file in your classpath is read (if present). Next, all settings from /persistence/
auction.cfg.xml are added and override any previously applied settings. Finally, an
additional configuration property (a default database schema name) is set programmatically,
and an additional Hibernate XML mapping metadata file is added
to the configuration.
You can, of course, set all options programmatically, or switch between different
XML configuration files for different deployment databases. There is effectively no
limitation on how you can configure and deploy Hibernate; in the end, you only
need to build a SessionFactory from a prepared configuration.
NOTE Method chaining—Method chaining is a programming style supported by
many Hibernate interfaces. This style is more popular in Smalltalk than
in Java and is considered by some people to be less readable and more
difficult to debug than the more accepted Java style. However, it’s convenient
in many cases, such as for the configuration snippets you’ve seen in
this section. Here is how it works: Most Java developers declare setter or
adder methods to be of type void, meaning they return no value; but in
Smalltalk, which has no void type, setter or adder methods usually return
the receiving object. We use this Smalltalk style in some code examples,
but if you don’t like it, you don’t need to use it. If you do use this coding
style, it’s better to write each method invocation on a different line. Otherwise,
it may be difficult to step through the code in your debugger.
Now that you know how Hibernate is started and how to build a SessionFactory,
what to do next? You have to create a configuration file for Hibernate.

_________________
_________________
Siegbert

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 4:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
not true guys ;) (what you pasted is just the *default* behavior)

You can use <mapping file="somefilepath"/> or simply do it programmatically.

But why on earth can't you have the mapping files inside a jar that is on the classpath ?

_________________
Max
Don't forget to rate


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