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?