Mistake
In the one-to-many example, many developers declared the JPA cascade options as following :
... @Entity @Table(name = "stock", catalog = "mkyong", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"), @UniqueConstraint(columnNames = "STOCK_CODE") }) public class Stock implements java.io.Serializable { ... private Set<StockDailyRecord> stockDailyRecords = new HashSet<StockDailyRecord>(0); @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST,CascadeType.MERGE }, mappedBy = "stock") public Set<StockDailyRecord> getStockDailyRecords() { return this.stockDailyRecords; } public void setStockDailyRecords(Set<StockDailyRecord> stockDailyRecords) { this.stockDailyRecords = stockDailyRecords; } ...
Save it with Hibernate session.
stockDailyRecords.setStock(stock); stock.getStockDailyRecords().add(stockDailyRecords); session.save(stock); session.getTransaction().commit();
What the code trying to do is when you save a “stock”, it will save the associated stockDailyRecords as well. Everything look fine, but THIS IS NOT WORKING, the cascade options will not execute and save the stockDailyRecords. Can you spot the mistake?
Explanation
Look in the code, @OneToMany is from JPA , it expected a JPA cascade – javax.persistence.CascadeType. However when you save it with Hibernate session, org.hibernate.engine.Cascade will do the following checking …
if ( style.doCascade( action ) ) { cascadeProperty( persister.getPropertyValue( parent, i, entityMode ), types[i], style, anything, false ); }
The Hibernate save process will causing a ACTION_SAVE_UPDATE action, but the JPA will pass a ACTION_PERSIST and ACTION_MERGE, it will not match and causing the cascade failed to execute.
@see source code
- org.hibernate.engine.Cascade
- org.hibernate.engine.CascadeStyle
- org.hibernate.engine.CascadingAction
Solution
Delete the JPA cascade – javax.persistence.CascadeType, replace it with Hibernate cascade – org.hibernate.annotations.Cascade, with CascadeType.SAVE_UPDATE.
... @Entity @Table(name = "stock", catalog = "mkyong", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"), @UniqueConstraint(columnNames = "STOCK_CODE") }) public class Stock implements java.io.Serializable { ... private Set<StockDailyRecord> stockDailyRecords = new HashSet<StockDailyRecord>(0); @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock") @Cascade({CascadeType.SAVE_UPDATE}) public Set<StockDailyRecord> getStockDailyRecords() { return this.stockDailyRecords; } public void setStockDailyRecords(Set<StockDailyRecord> stockDailyRecords) { this.stockDailyRecords = stockDailyRecords; } ...
Now, it work as what you expected.
Conclusion
It look like an incompatible issue between JPA and Hibernate cascade annotation, if Hibernate is a JPA implementation, what may causing the misunderstand in between?
Popularity: 1% [?]
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
RSS Feed
Twitter

февруари 7th, 2010
admin
Posted in 

i might not have thought this had been cool a number years in the past then again it’s crazy precisely how years adjusts the manner by which you experience all sorts of ideas, many thanks regarding the blog post it truly is great to read some thing intelligent now and then instead of the usual nonsense mascarading as blogs on the web,im going to play with a couple myspace poker chips regards
This might be of help…
I believe you might enjoy what I found on this webpage today….