-->
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.  [ 14 posts ] 
Author Message
 Post subject: Multiple projects using the same hibernate files?
PostPosted: Mon Sep 26, 2005 2:58 pm 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
I have multiple web projects that depend on a common JAR which houses a lot of commonly used classes.

For some applications that share DB classes (such as a User class for logging in to the admin side) I'd like to put the hibernate class files.

I need help on how to go about this. Let's go with this "User" example.

For the User.hbm.xml, I don't think I will have problems. The <hibernate-mapping package="...."> would be some package on the classpath, so that should be fine?

For hibernate.cfg.xml though, I'm a bit worried. The <mapping resource="/my/tool/package/User.hbm.xml"> looks like a path reference.

Since that XML would actually be inside a JAR file it would fail.

Could I get any help? Seems kind of cumbersome to have the User.hbm.xml in each project and the class files too. If say, a new field is added, all projects need to be updated :|

Thanks,
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 6:06 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Jar up the classes, along w/ hibernate.cfg.xml and all of the mapping files. when you call Configuration.configure(), H will find the hibernate.cfg.xml file if it is in the path, as well as the mapping files as if they were on disk.

Be careful of which environments you place this jar in, as you may load a configuration that you did not intend to load. In this case, you will need to call COnfiguration.configure(String), specifying the name of the cf.xml file to load.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 11:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Just to clarify, <mapping resource="..."/> instructs hibernate to perform a lookup as a classpath resource; notice the paths are relative from the classpath...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 10:50 am 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
OK, I think I might be getting somewhere with this then.

Here's one snag I've run into. Let's say I have my web application A, B, and then I have this common library we'll call CommonTools.

Before, A's servlet had a method

Code:
public static SessionFactory getSessionFactory()
   {
      if (Wireless.hibernateSessionFactory == null)   // Create a new one.
         hibernateSessionFactory = new Configuration().configure(CONFIG_FILE_LOCATION).buildSessionFactory();
      
      return hibernateSessionFactory;
   }


And in my Hibernate classes i'd add a .save() method, which would call this getSessionFactory and save it. However, it was inside application A so I knew what to call.

Now Person.save() could be called from either application since it's shared.

How should I be looking up the session factory? Would that thread-local pattern that I've read about work?

Thanks,
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 12:19 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
gcormier wrote:
Now Person.save() could be called from either application since it's shared.


The whole point of transparent persistence is so that we don't have to add methods like this to business objects. I promise your project will go much smoother if you stop doing this, and yes, you should probably invest some time in looking at HibernateUtil/HibernateFilter .


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 1:10 pm 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
But the persistance isn't truly transparent. If I write

Code:
Person aPerson = new Person();
aPerson.setName("greg");


At some point I need to write it to the database.

Wether or not that's calling aPerson.save(), or going through the lines of code to saveOrUpdate() the aPerson object, it's all the same, you are telling Hibernate at one point or another - Save this object to the database!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 1:12 pm 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
Sorry, just re-read your post and noticed you said "... add these kinds of methods to BUSINESS objects..."

:)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 3:02 pm 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
Being a noob and all, what are your thoughts on this..

I could make a DAO factory... which would spit out the proper DAO depending which application is called?

I'd still have to figure out how to actually do that, but on paper, does that sound like a good idea?

Thanks,
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 3:34 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Try this.

http://blog.hibernate.org/cgi-bin/blosxom.cgi/2005/09/08#genericdao


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 4:38 pm 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
Thanks! I'm having a problem.

I've made the HibernateUtil class.

Now, where do I put this and how do I set it up?

Inside, I changed the initialization line to

Code:
   sessionFactory = new Configuration().configure(CONFIG_FILE_LOCATION).buildSessionFactory();


Where CONFIG_FILE_LOCATION is a string to my config file. Problem is, that string changes depending on which application is currently running.

I tried moving this file into project B, figuring I'd copy it in project A with the different config string. The problem is that then the COMMON package, which holds PersonDAO, is unable to call this class since it can't find it. A and B depend on COMMON, not the other way around!

So I'm still a little bit stuck :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 4:43 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Read this, the documentation is inline.

http://www.hibernate.org/42.html

You can customize the behavior of the class using .properties, system properties, or inheritance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 11:22 am 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
Thanks everyone for your continued help in this matter :) I really apprieciate it as this is quite confusing.

Okay, I've tried using a properties file, but it doesn't work.

In my JSP if I have

Code:
HibernateUtil.currentSession();


I get an error saying it can't find the configuration file (loaded from my properties file)

However, if I copy and paste the initialization code in HibernateUtil directly into the JSP, it finds the hibernate.cfg.xml file no problem (same path since it's loaded from the properties file.)

HibernateUtil is in my COMMON package.

So I guess it's not able to look on the class path for the xml file? Is this path relative to the actual package then?

I still see no solution to this problem in sight :(

Thanks,
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 12:24 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
putting java in a JSP these days is another no-no ;) the application shouldn't be behaving that way, so I suggest you retrace your steps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 28, 2005 12:36 pm 
Beginner
Beginner

Joined: Thu Aug 18, 2005 4:34 pm
Posts: 33
Putting Java in the JSP was a logical step in verifying that my configuration is correct and works properly. I now know the problem lies in the fact that the COMMON jar file is unable to find the XML file in the parent project.


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