Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: hibernate-3.2
This is my scenario :
Item and Category are many-to-many relation , the owning side is Item.
Item and Image are one-to-many relation , Image(DELETE_ORPHAN) is dependent on Item :
Code:
In Item.java :
@OneToMany( fetch=FetchType.EAGER )
@Cascade(value = {CascadeType.SAVE_UPDATE , CascadeType.DELETE_ORPHAN} )
@JoinColumn(name="ItemID")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Image> images = new HashSet<Image>();
When I create a new Item , add Images , add Categories , and save it (using getHibernateTemplate().
save(newItem) ) ; the SQL dump is as following :
Code:
Hibernate: insert into Item (uuid, ....) values (?, ....)
Hibernate: insert into Image (data, size, ....) values (?, ?,...)
Hibernate: insert into Image (data, size, ....) values (?, ?,...)
Hibernate: insert into Image (data, size, ....) values (?, ?,...)
Hibernate: insert into Image (data, size, ....) values (?, ?,...)
Hibernate: insert into Item_Category (ItemID, CategoryID) values (?, ?)
Hibernate: update Image set ItemID=? where ID=?
Hibernate: update Image set ItemID=? where ID=?
Hibernate: update Image set ItemID=? where ID=?
Hibernate: update Image set ItemID=? where ID=?
I noticed that hibernate does redundant 'update' actions.
It seems that adding category to item makes hibernate think the item is updated , but it does not related to item's Image set...
How to avoid the redundant update actions ?
I tried to change the CascadeType to :
@Cascade(value = {
CascadeType.PERSIST , CascadeType.DELETE_ORPHAN} )
But it throws
TransientObjectException :
Code:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: foo.bar.Image
Maybe getHibernateTemplate().
persist(newItem) can solve the problem , but
persist doesn't return the generated identifier.
What should I do ?