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.  [ 5 posts ] 
Author Message
 Post subject: Auto-update of createdby and modifiedby fields
PostPosted: Tue Nov 15, 2005 10:06 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
In almost all my classes, I have two fields called "created by" and "modified by" that store, not suprisingly, the User object that is responsible for creating the object in the first place, and the user that last modified the object.

My question is this: What approach should I take so that these fields get updated on cascading save/update/delete actions?

I see a little bit about an EntityPersister in the documentation, but it isn't clear to me how the persister would know who the current User is.

The DAO object that is saving the root node of the graph certainly knows, and I could write something that would walk the graph and update them, but that seems error prone.

Is there something I'm missing, or a recommended approach for this problem?


Top
 Profile  
 
 Post subject: thread locals
PostPosted: Wed Nov 16, 2005 2:13 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
That may help
http://www.hibernate.org/318.html

And you do not have to use Asegy, simply assign user to a ThreadLocal variable in a filter/interceptor and use it.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 2:19 pm 
Newbie

Joined: Mon Sep 26, 2005 2:40 pm
Posts: 17
Location: Seattle
Brr, thread locals like this give me the heebie jeebies. They feel so much like global variables that are doing an end-run around encapsulation. I suppose there isn't much way around it.


Top
 Profile  
 
 Post subject: imo
PostPosted: Wed Nov 16, 2005 2:26 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
They are global variables. While I understand your shiver, I would not suggest dismissing them on that basis. ThreadLocals and InheritableThreadLocals are quite convenient and widely used, just be aware of implications and limitations.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: You could also look at using JTA
PostPosted: Wed Nov 16, 2005 6:15 pm 
Beginner
Beginner

Joined: Thu Dec 09, 2004 7:04 pm
Posts: 26
Location: Denver, CO
If you are in a managed environment, you could look at attaching the userid to the JTA transaction.

Because I'm in a weblogic environment, I simply used the extention to allows me to set properties against the transaction:
Code:
    protected void setTheCuid(String cuid) {
        javax.transaction.Transaction  tx = TransactionHelper.getTransactionHelper().getTransaction();
        Transaction wlstx = (Transaction) tx;
        wlstx.setProperty("user", user);
    }


Otherwise, you will have to use straight JTA calls to do exactly what getCurrentSession() does.

You will need to maintain a map of transactions to user ids as you get sessions.
You will also need to add yourself as a listener for the transaction complete events so you can remove the transaction->cuid map entries to keep you map size down and allow GC of the transaction objects.


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