-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can hbm.xml files be separated from their classes?
PostPosted: Mon Nov 07, 2005 6:58 pm 
Newbie

Joined: Mon Nov 07, 2005 6:49 pm
Posts: 3
I've been unable to search a/o Google for this answer...

I'm trying to put the *.hbm.xml files in a different package/directory than the referenced class file:

Code:
src:
   hbmXml:
      ClassName.hbm.xml
   com:
      yada:
         ClassName.java


Is this even feasible? Since I'm still going through growing pains, I'm using Hibernate 2.x, since that's what the available books reference.

Thanks!


Top
 Profile  
 
 Post subject: Yes
PostPosted: Mon Nov 07, 2005 7:04 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Mapping files have no file-system relationship to the package at all. So long as the package attribute of the hibernate-mapping element is correct (it must be the package of the class file), then it will work.

Some of the smaller projects in my company have chosen to put all mapping files together in one directory outside of the com.* package hierarchy. I don't personally like it, but there are no problems with it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:10 pm 
Newbie

Joined: Mon Nov 07, 2005 6:49 pm
Posts: 3
Hmm... If I relocate the file, how does it get found? For example, if I move the file to /src or to a new dir like /hbmXml, I get this:

Code:
Buildfile: D:\bunchr\workspace\Hibernate\build.xml
prepare:
compile:
qtest:
     [java] net.sf.hibernate.MappingException: Resource: com/oreilly/hh/Track.hbm.xml not found
     [java] at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:353)
     [java] at com.oreilly.hh.QueryTest.main(QueryTest.java:42)
     [java] Exception in thread "main"
     [java] Java Result: 1
BUILD SUCCESSFUL
Total time: 2 seconds


Top
 Profile  
 
 Post subject: Config files
PostPosted: Mon Nov 07, 2005 7:22 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Set it up in your main hibernate config file, the path to which is passed to your hibernate session factory method, something like:


Code:
  SessionFactory factSession =
   new org.hibernate.cfg.Configuration().configure(sPath).buildSessionFactory();


Then point sPath at your main config file that looks something like this:

Code:
<?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">
<hibernate-configuration>
    <!-- Hibernate config -->
     <session-factory>

    <property .../>
    <mapping resource="Path to a mapping file here"/>
    <mapping resource="Path to another mapping file here"/>
  </session-factory>
</hibernate-configuration>



There's loads of better docs than this elsewhere on this site and in your hibernate installation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:34 pm 
Newbie

Joined: Mon Nov 07, 2005 6:49 pm
Posts: 3
Coolness, I got it working!

Code:
       Configuration config = new Configuration();
       // config.addClass(Track.class); // removed this (conflicted, apparently)
       config.addResource("hbmXml/arbitraryPath/Track.hbm.xml"); // added this
       config.addProperties(props);


I'll have to work on this more, especially in using the better hibernate config XML file. I've been using hibernate.properties (more limited, it seems).

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 7:35 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You're welcome. And two more credits to me! Thanks.


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