-->
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.  [ 3 posts ] 
Author Message
 Post subject: Why hibernate set not-nul columns to null at cascade?
PostPosted: Tue Mar 06, 2012 1:08 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2010 2:30 am
Posts: 23
I use Hibernate.3.6.6.Final and SpringFramework and I have a problem with it. There is Playlist entity that has collection of PlaylistClip items. When I add or edit element of collection it works fine, but when remove item or try to change its position in list, Hibernate try to update not-null columns with null values, before remove action and fails.

Entities:
Code:
@Entity
@Table(name = "Playlists")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Playlist extends BaseEntityImpl<Integer> implements BaseEntity<Integer> {
   private static final long serialVersionUID = 5862404080695455375L;
   
   @Id   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column( name="Playlist_ID", unique = true, nullable = false, updatable = false )
   public Integer getId() { return super.getId(); }
   
   @OneToMany( fetch = FetchType.EAGER, cascade = {CascadeType.ALL} )   
   @OrderColumn( name = "position" )
   @JoinColumn( name="Playlist_ID" )
   public List<PlaylistClip> getClips() { return clips; }
   public void setClips( List<PlaylistClip> clips ) {
      this.clips = clips;
   }
   private List<PlaylistClip> clips;   
}


Code:
@Entity
@Table(name = "Playlists_Scenes")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class PlaylistClip extends BaseEntityImpl<Long> implements BaseEntity<Long> {
   private static final long serialVersionUID = 5862404080695455375L;
   
   @Id   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column( name="ID", unique = true, nullable = false, updatable = false)
   public Long getId() { return super.getId(); }
      
   @ManyToOne( fetch = FetchType.LAZY, optional = false, cascade = { CascadeType.ALL } )
   @JoinColumn(name = "Title_Scene_ID", nullable = false)
   public TitleScene getScene() { return scene; }
   public void setScene(TitleScene scene) {
      this.scene = scene;
   }
   private TitleScene scene;
   
   @Column(name = "Playlist_ID", nullable = false)
   public Integer getPlaylistId() { return playlistId; }
   public void setPlaylistId( Integer id ) {
      this.playlistId = id;
   }
   private Integer playlistId;
   
   public Integer getPosition() { return position; }
   public void setPosition(Integer position){ this.position = position; }
   private Integer position;
}


Code that fails ( getClips returns two clips, both with playlistId and positions 0 and 1):
Code:
List<PlaylistClip> clips = entity.getClips();         
clips.remove(  0 );
int index = 0;
for( PlaylistClip clip : clips ) {
   clip.setPosition( index );
   index++;
   }         
dao.saveOrUpdate( entity );


Code:
public class MutableDAOImpl<K extends Number, T extends BaseEntity<K>>  implements MutableDAO<K, T> {
    @Override
    @Transactional( readOnly = false, propagation = Propagation.SUPPORTS )
    public void saveOrUpdate( T entity ) {
       getSessionFactory().getCurrentSession().saveOrUpdate( entity );
    }
}

Since every playlistClip should be member of playlist, it should have position, so Playlist_ID and position are not nullable both in POJO and DB.
However I see exception when I try to remove or to change clip position. The exeception is:
"Cannot insert the value NULL into column 'Playlist_ID', table 'Anyclip2.dbo.Playlists_Scenes'; column does not allow nulls. UPDATE fails."

I did some debugging and I see that all clips in collection has palylistId, but when Hibernate flushes the session it has this update in actions queue.

Can anybody explain to me why Hibernate do that and how to fix it?


Top
 Profile  
 
 Post subject: Re: Why hibernate set not-nul columns to null at cascade?
PostPosted: Fri Mar 09, 2012 5:29 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2010 2:30 am
Posts: 23
Documentation and stackoverflow say that if element is removed from collection, hibernate considers it as remove of relationship but not of entity. However, there are many cases when one side in relationship cannot exist without other, so setting joinColumn value to null is impossible. What should be done? Is orphanRemoval will work or it works only from parent to child and not back?


Top
 Profile  
 
 Post subject: Re: Why hibernate set not-nul columns to null at cascade?
PostPosted: Sun Mar 11, 2012 11:32 am 
Beginner
Beginner

Joined: Thu Jun 24, 2010 2:30 am
Posts: 23
I still need help. I tried 'orphanRemoval' and it does not work. It adds delete action to batch, but update action is still there, so constrain vioaltion occured before delete.


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