-->
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.  [ 2 posts ] 
Author Message
 Post subject: save() can change Collection fields
PostPosted: Sun Jan 09, 2005 3:32 am 
Newbie

Joined: Thu Nov 04, 2004 10:24 am
Posts: 17
FYI:

I just caught an interesting bug in my code, ultimately caused by Hibernate.

A List field (mapped as a list of elements) was not getting written to DB.

It turns out that during save(), Hibernate uses reflection to call setter methods on fields of List type (likewise for other Collection), so the original List field is replaced by a special Hibernate List, which wraps the original one. Unfortunately, our setter method did not just assign to the field as one might expect, but rather cleared the existing List and added elements to it, thus:

Code:
setMyListField(List list){
    myListField.clear()
    myListField.addAll(list);
}


This was done to avoid referential integrity problems, as we don't want to share a reference to the parameter.

So, when Hibernate called the setter, it cleared not only the underlying field, but the parameter (Hibernate's List) as well -- since it wraps the underlying field!

Tricky, that one. We'll need to keep our eyes open for similar problems wherever a setter or getter does more than simply set/get a field.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 09, 2005 9:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is documented in the reference documentation.


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