views:

52

answers:

1

My model:

 public class Father {
  Set<Son> sons = new HashSet<Son>();
  String name = null;
  Date lastSonModifyDate = null;
  // ... other fields and setters/getters
 }
 public class Son {
  Father father = null;
  String name = null;
  Date lastModifyDate = null;
  // ... other fields and setters/getters
 }

Use case:

  1. There is in DB a Father object with a Son object associated (bidir).
  2. Load from DB father.
  3. Update name field for father.
  4. Update name field for son.
  5. Persist father.

My interceptor first detects father updates (onFlushDirty). Then executes the onFlushDirty for the son. In this case, I update son.lastModifyDate and also father.lastSonModifyDate.

When execution ends, all updates are persisted except father.lastSonModifyDate. I think this is because father is in session and has been updated before son, so this entity overrides the changes done in onFlushDirty method for the son entity.

How could I achieve my mark (set father's lastSonModifyDate from son interceptor)?

Thanks.

A: 

You can't. onFlushDirty() is invoked for collection elements after it has been invoked for owner and update action (if any) has already been scheduled.

Is there any reason why you can't do all of the above in your DAO instead of relying on interceptors? Or on the database level (mapping both lastModifyDate properties as generated)?

ChssPly76
I can't do that in DAOs because Son objects, generally, are saved/updated by cascade from Father objects... so I can't to control it.Perhaps database level is a nice solution. But, "generated" means that database engine creates the times tamp? really I need set the date attributes in a concrete format, not supported by any db engine...
Alberthoven
Not sure what you mean by "updated by cascade ... so I can't control it". If you have, say, an `updateFather()` method in your DAO, what's to prevent you from timestamping any entities you want _before_ invoking session.update()? As far as database goes, what does "concrete format" mean? A date is a date (time / timestamp, whatever), it's saved as such. How you format it in your app is a totally different story.
ChssPly76
Ok, fisrt, I almost never call DAO.saveOrUpdate for Son objects, but only for Father objects... so Son elements associated to a Father are persisted in cascade (with org.hibernate.annotations.Cascade).Second, I'm afraid I haven't been completely sincere about date fields: actually, Father.lastSonModifyDate and Son.lastModifyDate represents dates, but their type is String, because a use a String DateFormatter to read and write their values...This is the reason because the format is my "not supported".
Alberthoven
That's not what I meant. When you call dao.save(father) from your business (service) layer, why can't you update both your father and appropriate son instances before calling session.saveOrUpdate(father)? As far as lastModifyDate goes, do your store them as strings in your database? Why? That's just asking for trouble. But if if that's a legacy schema you can't change, vast majority of databases have some sort of date_format() function that would do render the date according to your format.
ChssPly76
Yes, I think I will do like you tell.Really I don't know why we use String for dates, it was not my decision... perhaps we should change it.Thanks for the answers. Regards.
Alberthoven