-->
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: Hibernate and JBOSS Example
PostPosted: Thu Apr 01, 2004 1:15 pm 
Newbie

Joined: Wed Jan 28, 2004 10:47 am
Posts: 3
Hi,

Could someone possibly point me in the direction of a complete JBOSS/Hibernate example?

I've come up the learning curve of using Hibernate and JBOSS individually, but need that last bit of help to pull them together.

Many thanks

Gordon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 1:31 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Have you read this yet?

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 1:44 pm 
Newbie

Joined: Wed Jan 28, 2004 10:47 am
Posts: 3
Yes, this has gotten me most of the way there (I think?).

I've created an entity bean and mapping file, and have tested this using a stand-alone application (generated the schema from the mapping file - all works ok).

I've set up my jdbc connection and tested this as working using the J2EE tutorial CMP example.

I've packaged up a SAR as described in the above article. JBOSS didn't throw up any errors when I deployed the EAR, so I'm assuming it's working ok.

I've created a jboss session bean which simply returns a number, and this works ok.

So now for the last bit...how do I call the hibernate service from within the session bean?

I had the following code in my stand-alone application, and this worked fine:

Configuration configuration = new Configuration();
configuration.addClass(Trade.class);
SessionFactory sessionFactory=configuration.buildSessionFactory();
Trade newTrade = new Trade();
newTrade.setTradeName("Trade1");
Session session = sessionFactory.openSession();
session.save(newTrade);
session.flush();
session.connection().commit();
session.close();

When I copy this into the session bean code the IDE (I'm using Eclipse) is giving me an error "Unhandled exception type NamingException" on most of these lines starting at configuration.addClass.

I'm a newbie at Java as well, so apologies in advance if I'm asking something really dumb.

Many thanks

Gordon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 2:04 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Quote:
I'm a newbie at Java as well, so apologies in advance if I'm asking something really dumb.

You're biting off quite a chunk, aren't you? :)

Some java methods can throw Exceptions. When that is the case you have to either place the method call in a try/catch block or rethrow the exception from the current method.

If the preceeding paragraph was incomprehensible you need to do a bit more study...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 3:34 pm 
Newbie

Joined: Tue Dec 09, 2003 4:45 pm
Posts: 14
Hi Gordon,

If you've followd the SAR example and deployed the SAR within JBoss, then there are a couple of things you'll want to change w.r.t. how you use Hibernate from within your session bean method call.

1. You don't have to manually create the configuration and session factory. If the SAR is done properly, this all gets taken care of.

2. If I recall, the SAR example has an attribute element that defines a JNDI name for the hibernate session factory. Lets say you used an attribute value of "java:/hibernate/HibernateFactory". You can get the session factory from JNDI like this (keep in mind that this isn't production quality or anything...):

Code:
try {
InitialContext ctx = new InitialContext();
SessionFactory factory = (SessionFactory)ctx.lookup("java:/hibernate/HibernateFactory");
}
catch (Exception ex) {
//do something...
}

Once you've got the factory, you can create sessions and do the rest of your Hibernate work in the normal way.

Good luck, Gordon.

_________________
Mike Davison


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 01, 2004 8:36 pm 
Newbie

Joined: Wed Jan 28, 2004 10:47 am
Posts: 3
It's taken a while, but I finally got there!
Thanks for the help.

Gordon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 2:48 pm 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Hi all,

I created the following into HibernatePlugin, according mdavision, but
an exception occurs.

Code:
InitialContext ctx = new InitialContext();
this.factory = (SessionFactory)ctx.lookup("java:/hibernate/HibernateFactory");



The exception:

Code:
javax.naming.NameNotFoundException: hibernate not bound


What is the problem with this?

thanks

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 6:37 pm 
Newbie

Joined: Thu Apr 01, 2004 6:28 pm
Posts: 15
jboss-service.xml deve ta errado


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 7:35 am 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Hi, I found the error... it was in the following tag:

<depends>jboss.jca:service=LocalTxCM,name=DataSourcr</depends>

where the correct is:

<depends>jboss.jca:service=LocalTxCM,name=projds</depends>



thanks strikepollar

[]

_________________
Tads


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.