Rollback persistence with Hibernate

by @im_a_muppet on November 17, 2007

So I was implementing a Rounds entity that had a Games entity as one of its members. To persist a Rounds I first persisted the games and then got the games back from the model and added them to the Round and committed the Rounds.

This however would not allow me to rollback if there was an error in the Rounds creation. I had to manually delete the Games created for the Rounds.

To simplify this you can use the cascade option in the hbm file. This way you can just add the un-persisted Games to the round and save the round. This will automatically create the persisted versions of all Games attached to the Rounds.

Here is my hbm excerpt

<set name="games" table="rounds_games" lazy="false" 
                   cascade="all,delete-orphan">
   <key>
         <column name="round_id" not-null="true" />
   </key>
   <many-to-many column="game_id" unique="true"
                  class="com.willspics.cs.epl.model.data.Games" /> 
</set>

More information can be found by clicking here

blog comments powered by Disqus
Tweet