Hi,
When I save my "site" object, hibernate issues an update statement for each of my items in my OneToMany Set. There is a OneToMany relationship between site and siteattraction.
All I do to trigger these updates is to initialize the set and print out how many there are in the set.
I thought that Hibernate would only update those records that I changed in the set. It appears that Hibernate is marking the entire collection as dirty.
What I would like to know if this is the way Hibernate is supposed to work (update the entire set), or selectively update changed records of a set. This would help me decide the future of how I do mapping in my application.
Thank you!!!
Phillip
Hibernate version:
3.2.1
Mapping documents:
Code:
This is the OneToMany relationship:
private Set<SiteAttraction> siteAttractions = new HashSet<SiteAttraction>();
@OneToMany(mappedBy = "site")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public Set<SiteAttraction> getSiteAttractions() {
return siteAttractions;
}
public void setSiteAttractions(Set<SiteAttraction> siteAttractions) {
this.siteAttractions =siteAttractions;
}
This is the SiteAttraction class:
Code:
@Entity
@Table(name = "site_attraction2")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SiteAttraction extends BaseDBObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private long siteAttractionId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "site_attraction_id")
public long getSiteAttractionId() {
return (siteAttractionId);
}
/**
* The setter method for this object identifier.
*/
public void setSiteAttractionId(long siteAttractionId) {
this.siteAttractionId = siteAttractionId;
}
@Transient
public Long getId() {
return new Long(getSiteAttractionId());
}
private Attraction attraction = null;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name="attraction_id", nullable = false)
public Attraction getAttraction() {
return attraction;
}
public void setAttraction(Attraction attraction) {
this.attraction = attraction;
}
private Site site = null;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name="site_id", nullable = false)
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
public boolean equals(Object obj) {
return obj.hashCode() == this.hashCode();
}
@Transient
public int hashCode() {
String key = attraction.getObjectIdString() + ":" + site.getObjectIdString();
return key.hashCode();
}
private boolean published = false;
@Column(name = "published")
public boolean getPublished() {
return published;
}
public void setPublished(boolean published) {
this.published = published;
}
}
The generated SQL (show_sql=true):
update
site_attraction2
set
viewable=?,
create_date=?,
modify_date=?,
site_id=?,
attraction_id=?,
published=?
where
site_attraction_id=?
Debug level Hibernate log excerpt:
15:16:45,009 DEBUG AbstractEntityPersister:1942 - Dehydrating entity: [com.reffects.dmi.dbom.SiteAttraction#9856452]
15:16:45,009 DEBUG BooleanType:80 - binding 'true' to parameter: 1
15:16:45,009 DEBUG CalendarType:80 - binding '2007-08-16 12:18:38' to parameter: 2
15:16:45,009 DEBUG CalendarType:80 - binding '2007-08-16 12:18:38' to parameter: 3
15:16:45,009 DEBUG LongType:80 - binding '3701728' to parameter: 4
15:16:45,009 DEBUG VersionValue:44 - version unsaved-value strategy UNDEFINED
15:16:45,009 DEBUG IdentifierValue:104 - id unsaved-value: 0
15:16:45,009 DEBUG LongType:80 - binding '8471887' to parameter: 5
15:16:45,009 DEBUG BooleanType:80 - binding 'false' to parameter: 6
15:16:45,009 DEBUG LongType:80 - binding '9856452' to parameter: 7
15:16:45,009 DEBUG VersionValue:44 - version unsaved-value strategy UNDEFINED
15:16:45,009 DEBUG IdentifierValue:104 - id unsaved-value: 0
15:16:45,025 DEBUG NonstrictReadWriteCache:116 - Invalidating: com.reffects.dmi.dbom.SiteAttraction#9856452
15:16:45,025 DEBUG UpdateTimestampsCache:50 - Pre-invalidating space [site_attraction2]
15:16:45,025 DEBUG AbstractEntityPersister:2289 - Updating entity: [com.reffects.dmi.dbom.SiteAttraction#9122801]
15:16:45,025 DEBUG AbstractBatcher:222 - reusing prepared statement
15:16:45,025 DEBUG SQL:393 -