-->
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.  [ 8 posts ] 
Author Message
 Post subject: passing information to hibernate layer
PostPosted: Mon Aug 25, 2008 12:53 pm 
Newbie

Joined: Mon Aug 25, 2008 8:39 am
Posts: 10
Is there a way to pass information to hibernate layer in a generic way, like adding object to current session/transaction.

I have a hibernate interceptor which logs some information to db, I would like to pass user name/ transaction id information from application layer to hibernate layer without adding this information to exiting persisting objects.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 3:20 pm 
Beginner
Beginner

Joined: Fri Aug 05, 2005 3:36 am
Posts: 28
I'm not sure I understand your question; maybe if you wrote some pseudo-code it would be a little clearer.

But the Session interface has a lot of methods that might serve your needs, like get, find, merge, etc.

-larry


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 4:17 pm 
Newbie

Joined: Mon Aug 25, 2008 8:39 am
Posts: 10
Here is what I am trying to do.

Lets say application code does following

/*** application code begins ***/
session.beginTransaction();
myDAO.saveSomething(myPesistentObj);
yourDAO.saveSomethingElse(yourPesistentObj);

// I would like to have some object in run time, where I can store information
// like USER NAME, TRANSACTION ID, SOME OTHER INFO
/ This information later should be accessible by hibernate interceptor
session.endTransaction();

/*** application code ends **/



I have written a interceptor modeled after Audit Log example on hibernate site. Hibernate Interceptor code has following method


public void postFlush(Iterator iterator) {
// Here BEFORE INSERTING THE LOG RECORD I WOULD LIKE
// to access the information I had stored in some object while
// executing above application code.

for (Iterator itr = inserts.iterator(); itr.hasNext();) {
LogRecord logRecord = (LogRecord) itr.next();
session.save(logRecord);
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 7:07 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
If you're using Spring, this can be achieved with a custom-scoped or session-scoped spring bean (of source, you'll have to get access to the Spring app context from the listener, but htat's your deal).

If you're rolling your own infrastructure, most people achieve this with a static ThreadLocal.

I'm not sure if Hibernate has any actual support for this build in.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2008 12:24 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
I don't know if you know this but you could set the interceptor at Session level. So everytime you create a new Session you pass a new Interceptor which has the User Name and Transacction ID.

Interceptor interceptor = new MyInterceptor (username, transactionId);
session = sessionFactory.openSession (interceptor);

NĂ©stor


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 1:06 pm 
Newbie

Joined: Mon Aug 25, 2008 8:39 am
Posts: 10
stolley, nestorjb,

Thanks for the help. Both the approaches will work. I am using spring so do can not instantiate Interceptor .

I am using a request scoped bean to pass the information, and Interceptor has to implement ApplicationContextAware to read request scoped stuff from the context.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 6:50 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
When I was doing what you're doing (from a JPA listener) - I just jammed the ApplicationContext into a static variable on the listener at application boot-up time (from a Servlet Lifecycle listener).

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 11:48 pm 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
vjhiber wrote:
Here is what I am trying to do.

Lets say application code does following

/*** application code begins ***/
session.beginTransaction();
myDAO.saveSomething(myPesistentObj);
yourDAO.saveSomethingElse(yourPesistentObj);

// I would like to have some object in run time, where I can store information
// like USER NAME, TRANSACTION ID, SOME OTHER INFO
/ This information later should be accessible by hibernate interceptor
session.endTransaction();

/*** application code ends **/



I have written a interceptor modeled after Audit Log example on hibernate site. Hibernate Interceptor code has following method


public void postFlush(Iterator iterator) {
// Here BEFORE INSERTING THE LOG RECORD I WOULD LIKE
// to access the information I had stored in some object while
// executing above application code.

for (Iterator itr = inserts.iterator(); itr.hasNext();) {
LogRecord logRecord = (LogRecord) itr.next();
session.save(logRecord);
}

}


well you can also extend the session class and can provied you specific implementation there also


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