-->
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: Audit Info Custom Type
PostPosted: Fri Sep 12, 2003 8:04 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
I am questioning the validity of this design pattern in the community area (http://www.hibernate.org/48.html). I thought you couldn't
modify the entity in Interceptor. I've tried the following (even though the docs say don't) and I know that the changes made don't make it to db.

Code:
if( entity instanceof Auditable ) {
      MyEntity vo = (MyEntity) entity
      try {
          Timestamp timestamp = new Timestamp(System.currentTimeMillis());
          vo.setLastUpdated(timestamp);
      }
      catch( Exception e ) { e.printStackTrace();}
  }
  return false;

Does the design pattern "magic" (AuditInfoType, final class AuditInfo, etc..) provide a way around this limitation? If so then why?

jeff.boring@siemens.com


Top
 Profile  
 
 Post subject: Re: Audit Info Custom Type
PostPosted: Fri Sep 12, 2003 8:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
jwboring wrote:
I am questioning the validity of this design pattern in the community area (http://www.hibernate.org/48.html). I thought you couldn't
modify the entity in Interceptor. I've tried the following (even though the docs say don't) and I know that the changes made don't make it to db.

Code:
if( entity instanceof Auditable ) {
      MyEntity vo = (MyEntity) entity
      try {
          Timestamp timestamp = new Timestamp(System.currentTimeMillis());
          vo.setLastUpdated(timestamp);
      }
      catch( Exception e ) { e.printStackTrace();}
  }
  return false;

Does the design pattern "magic" (AuditInfoType, final class AuditInfo, etc..) provide a way around this limitation? If so then why?

jeff.boring@siemens.com


The pattern works because AuditInfo is it's own type - you can change AuditInfo directly, NOT basic properties as you do in your example.

If want to change those properties you need to do it in the values array sent as a parameter to your interceptor.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 8:45 am 
Beginner
Beginner

Joined: Tue Aug 26, 2003 1:02 pm
Posts: 34
Location: Akron, OH
Here's my Interceptor's onFlushDirty method:
Code:
if (entity instanceof Auditable) {
    AuditInfo ai = ((Auditable) entity).getAuditInfo();
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    ai.setLastUpdated(timestamp);
    ai.setUpdatedBy(user);
}
return false;

However, I haven't tried an update yet. We are doing the cast and the assignment differently. I'll try an update and let you know if it makes it to the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 9:46 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
Thanks Max. I know about the values array but this pattern eliminates the looping - we've got some really big tables (lots of cols). Of course, this pattern might take longer than the looping :-)

Hey, what's the final business about? Is this mandatory?

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
jwboring wrote:
Thanks Max. I know about the values array but this pattern eliminates the looping - we've got some really big tables (lots of cols). Of course, this pattern might take longer than the looping :-)


If the audit fiels is in a AuditType then you won't have any problems !?
You are not following the design pattern - you are having the fields directly on the entity!

Quote:
Hey, what's the final business about? Is this mandatory?

Jeff


The final what ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:09 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
No Max, I will follow the pattern and use the custom type. My code was only listed as an example of what I thought you couldn't do. Your excellent & timely reply helped me understand this. Thanks,

Quote:
The final what ?

Code:
public final class AuditInfo implements Serializable {
       private Timestamp lastUpdated ;
       private Timestamp created ;
       private Long updatedBy ;
       private Long createdBy ;
   
       public Timestamp getLastUpdated()                 { return lastUpdated;  }
       public void setLastUpdated(Timestamp lastUpdated) { this.lastUpdated = lastUpdated; }
       public Timestamp getCreated()                     { return created; }
       public void setCreated(Timestamp created)         { this.created = created; }
       public Long getUpdatedBy()                        { return updatedBy; }
       public void setUpdatedBy(Long updatedBy)          { this.updatedBy = updatedBy; }
       public Long getCreatedBy()                        { return createdBy; }
       public void setCreatedBy(Long createdBy)          { this.createdBy = createdBy; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I would think it is a pretty good thing that your auditinfo class cannot be messed with by subclasses - you don't ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:36 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
OK, point taken. Thanks again! This real time support is really great. Why don't you just give me your phone number :-)

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 10:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
jwboring wrote:
OK, point taken. Thanks again! This real time support is really great. Why don't you just give me your phone number :-)

Jeff


Because that you cannot afford ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Audit Info Custom Type
PostPosted: Wed Oct 10, 2007 12:07 am 
Newbie

Joined: Tue Oct 09, 2007 11:53 pm
Posts: 5
Is this still the accepted method to create an Audit, or has a more standard method come along in the last few years that works better?

There are many more methods in UserType and Interceptor now, some of which look very useful, and the signature for Hibernate.TIMESTAMP.deepCopy() has changed.


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.