-->
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.  [ 7 posts ] 
Author Message
 Post subject: Common Fields update
PostPosted: Mon Jul 27, 2009 6:36 am 
Newbie

Joined: Fri Dec 14, 2007 8:44 am
Posts: 10
Dear All,

OK, might be some of you would find it an interesting topic.
I was consedering about a situation detailed below,

1. In normal db, we keep track for each table via masters, adding/having same named fields
like
create_author,
create_date,
update_author,
update_date,
update_counter

now, I was wondering about the tooling(automatic update).
Despite one could set() each of all fields in service class and do save(),
can it be possible to automate the process so that for every save()/update() operation above fields should get automatically updated and need not to do individualy for each db_table.

Any idea !!! please share your advice.


Top
 Profile  
 
 Post subject: Re: Common Fields update
PostPosted: Fri Jul 31, 2009 4:27 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
sure it's possible, when using JPA there are "Life Cycle Callback Methods", you could add a method on the entity itself to perform the changes you need, and annotate it like @PrePersist

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Common Fields update
PostPosted: Fri Aug 07, 2009 9:13 am 
Newbie

Joined: Fri Dec 14, 2007 8:44 am
Posts: 10
Hi Sanne,

nice reply !!
but I think, this approach would be infeasible when functioning with hundreds of entities.
Needs code duplication, and would be intricate to distinguish between save() and update() operations. I mean, for save() all fields would be inserted, but for update() only update_author, update_date and update_counter and would be updated.

I presume to perform this job using an interceptor,
Just add once and should act upon each entity, also should judge save() and update() operations.

In there any way!!
--------------------
Ceezane


Top
 Profile  
 
 Post subject: Re: Common Fields update
PostPosted: Fri Aug 07, 2009 9:28 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
if it's the same logic you want to apply on many entities you could use an eventlistener, the're globally registered in Hibernate on the specific events you care for.
You might also be interested in Envers, the new plugin to enable auditing.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Common Fields update
PostPosted: Tue Aug 11, 2009 1:14 am 
Newbie

Joined: Fri Dec 14, 2007 8:44 am
Posts: 10
Hi,

that’s jingle nice.
could you please let some briefings on it,
It would be an immense for my auxiliary investigation.
Thanks !!

__________
ceezane


Top
 Profile  
 
 Post subject: Re: Common Fields update
PostPosted: Tue Aug 11, 2009 4:45 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Sure;
there are different interfaces you can implement, depending on which event you want to listen to, like
PostDeleteEventListener, PostInsertEventListener, PostUpdateEventListener, ...
All of Hibernates internals are implemented by these events (like saving an entity is actually performed by such a listeners), and you can add more listeners and even replace de default ones.

http://fisheye.jboss.org/browse/Hibernate/search/trunk/src/main/java/org/hibernate/search/event/FullTextIndexEventListener.java?r=11631 is an example of a fairly complex listener, it's the one for Hibernate Search.

This can be enabled by code:
http://fisheye.jboss.org/browse/Hibernate/search/trunk/src/main/java/org/hibernate/search/event/EventListenerRegister.java?r=16306

or by XML configuration:
http://docs.jboss.org/hibernate/stable/search/reference/en/html_single/#d0e1210

just replace the classname with your own implementation; if you register them by code make sure to ADD your listeners to the array, not to replace the existing. Also remember the EventListener is shared across all Session(s), so. make sure it's threadsafe or stateless.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Common Fields update
PostPosted: Thu Aug 13, 2009 12:42 am 
Newbie

Joined: Fri Dec 14, 2007 8:44 am
Posts: 10
Thanks a lot,
I will do some touch and will post again, if needed. Thank you!!


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