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.  [ 10 posts ] 
Author Message
 Post subject: Problems Hibernate App in jar
PostPosted: Thu Aug 03, 2006 6:00 am 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
3.1

Hi,

I am currently working on a small project with Hibernate and Struts and use NetBeans with HibernateTools for automatically generating my HibernateBeans.

But since I want to develope this application very independent to Struts I split it up in two parts:
The Hibernate Application with its Facades and Beans
The Struts WebApplication

But in order to use the HibernateFacades I include the .jar package into my WepApplication Folder.

Now if I run the webapplication and want to call some Hibernate function there is always this stupid error that "someName".hbm.xml cannot be found, (I'm currently not at home so I cant post the real message, but it is some kind of Mapping error) although the .hbm.xml files are included in the .jar

If I run the Hibernate Application seperately everything works great.

Do I have to add a path to the classpath???

Regards,
Peter


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 12:30 pm 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
Now here is the Exception:
As I wrote, the hbm.xml is actually in the .jar but why can't it be found???

java.lang.ExceptionInInitializerError
........
Caused by: org.hibernate.MappingException: Resource: com\giz\basis\model\UserBO.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:479)
com.giz.basis.util.HibernateUtil.getSessionFactory(HibernateUtil.java:32)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 12:46 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
if you use hibernate.cfg.xml, you should have following element

Code:
<mapping jar="your_hibernate_app.jar"/>


or if you configure programmatically use following line to load your mappings

Code:
Configuration.addJar( ... );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 1:51 pm 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
Hi,

thank you for your reply,
but unfortunately it did not solve my problem, the same exception is thrown again.

Where do I have to place this code.

I actually want my web application to be totally independent of Hibernate but now I have placed the code in an ActionForm before I call an Insert Method of a Service layer which then calls the methods of the HibernateFacade.

Here is my code in the ActionForm:


Configuration conf = new Configuration();
conf.addJar(new File("D:/Basis.jar"));
System.out.println("addAction");
UserDTO user = new UserDTO();

BeanUtils.copyProperties(user,form);

IBasicServices service = new IBasicServcisesImpl();
service.insertUser(user)

return mapping.findForward("add");


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 2:29 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I have the following web-app structure which can help you.


WEB-INF/classes/...
WEB-INF/lib/...
my-hibernate-mappings.jar
hibernate.cfg.xml
log4j.properties

within hibernate.cfg.xml file I have defined mapping element as

Code:
<mapping jar="/my-mappings.jar"/>


this works fine for me. And I configured on the server for shared libraries that are required at runtime.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 3:03 pm 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
Hi,

thank you for your hints. I tried it but it still does not work.

I'd like to explain you more details about my jar.
It contains the following packages and classes
model: User.class User.hbm.xml UserFacade.class
util: HibernateUtil (generates the session)
dto: UserDTO.class
services: BasicServices

all the configuration files are generated while compiling the package.
But it is intended to be a self-sufficient application (Struts should not know that Hibernate is the provider of the data)

so your approach with the cfg.xml-file is not really clear to me. And how "struts knows?" or "hibernate knows?" were the mapping files are?
(I'm very new to hibernate as you may have realised)

Thank you for your patience!


Last edited by peterson on Thu Aug 03, 2006 3:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 3:04 pm 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
Hi,

thank you for your hints. I tried it but it still does not work.

I'd like to explain you more details about my jar.
It contains the following packages and classes
model: User.class User.hbm.xml UserFacade.class
util: HibernateUtil (generates the session)
dto: UserDTO.class
services: BasicServices

all the configuration files are generated while compiling the package.
But it is intended to be a self-sufficient application (Struts should not know that Hibernate is the provider of the data)

so your approach with the cfg.xml-file is not really clear to me. And how "struts knows?" or "hibernate knows?" were the mapping files are?
(I'm very new to hibernate as you may have realised)

Thank you for your patience!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 3:31 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
You can use following code to build session factory in HibernateUtil class.

Code:
Configuration c = new Configuration();
c.addJar( new File( "/my-mappings.jar" ) );
Properties p = new Properties();
p.put( "hibernate.connection.driver_class", "oracle.jdbc.OracleDriver" );
p.put( "hibernate.connection.url", "YOUR_DB_URL" );
p.put( "hibernate.connection.username", "USER" );
p.put( "hibernate.connection.password", "PASSWORD" );
p.put( "hibernate.transaction.auto_close_session", "false" );
p.put( "hibernate.connection.pool_size", "1" );
p.put( "hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect" );
p.put( "hibernate.current_session_context_class", "thread" );
p.put( "hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider" );
p.put( "hibernate.show_sql", "true" );
p.put( "hibernate.format_sql", "true" );
p.put( "hibernate.use_sql_comments", "false" );
p.put( "hibernate.hibernate.type", "true" );
c.addProperties( p );
sessionFactory = c.buildSessionFactory();


Instead of adding jar file with mappings as in the second line of the code, you can also use

Code:
c.addResource( "User.hbm.xml" );//correct path is necessary


If you just use the code

Code:
Configuration c = new Configuration();
SessionFactory sfac = c.configure().buildSessionFactory();


hibernate expects to find hibernate.cfg.xml file in the root of the classpath.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 5:27 pm 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
Hey,

found a solution, but only a very messy one.
I've copied all *hbm.xml into the source folder of the web-app (into the package with the same name as in the .jar File).

Nonetheless I want the hbm's to be in the jar folder as well.

Now I can show you how I add them:

Code:
Configuration configuration = new Configuration();
                // load all beans
                InputStream is = HibernateUtil.class.getResourceAsStream("hibernateBeans.lst");
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line ;
                while ((line = reader.readLine()) != null) {
                    configuration.addResource(line);
                }
                Properties properties = new Properties();
                properties.load(HibernateUtil.class.getResourceAsStream("hibernate.properties"));
                configuration.setProperties(properties);
                sessionFactory =  configuration.buildSessionFactory();


in the hibernateBeans.lst file are the Paths to the Beans like: com/pip/User.hbm.xml
which is correct in the Hibernate Project but in the webapp it points to the root directory of the webapp and not into the .jar.

Do you know how I can change that, because the HibernateUtil is in the same .jar as the .hbm.xml


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 5:23 am 
Newbie

Joined: Thu Aug 03, 2006 5:50 am
Posts: 15
Hi,

I think I've found a solution for my problem but have not been able to test it yet (not at home again).

This should work:
you can pass a ClassLoader parameter to the addRessource method!

Code:
ClassLoader cl = ClassLoader.getSystemClassLoader()
Configuration conf = new Configuration();
conf.addRessource("blabla.hbm.xml",cl);


I hope that it will work. (I'll tell you on sunday)

Nonetheless, thank you for your hints!

BTW: found a similar problem in the java technology forum
http://forum.java.sun.com/thread.jspa?forumID=22&threadID=482005


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