-->
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.  [ 2 posts ] 
Author Message
 Post subject: delete objects in list when updating
PostPosted: Fri Nov 21, 2008 1:21 am 
Newbie

Joined: Wed Apr 30, 2008 4:23 am
Posts: 4
Hi

I am a newbie to hibernate and I'm using hibernate annotations for my project. The entity classes are as follows

Code:
@Entity
public class Artist
{
Integer artistId;
String artistName;
............
List<Album> albumList;

@OneToMany(mappedBy = "artist", cascade = CascadeType.ALL)
   public List<Album> getAlbumList() {
      return albumList;
   }

@Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public Integer getArtistId() {
      return artistId;
   }

..........................

}

@Entity
public class Album
{
   Integer id;
   Artist artist;


@Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public Integer getId() {
      return id;
   }

@ManyToOne
   @JoinColumn(name = "artistId")
   public Artist getArtist() {
      return artist;
   }


.....................

}


..........................
hibernateSession.update(artist);
..........................



I have a problem when updating an 'artist' object. The mapped 'album' objects also gets updated but they do not get deleted according to the number of objects in the 'albumList'
eg: if 3 albums were added to an artist and later on if one album is removed it is not reflected in the db when updating an 'artist' object. but if an album name is changed or a new album is added, that change is reflected in the db when update(artist) is called.

what should be done to remove records when updating an object in this scenario

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2008 5:17 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
The problem is that Artist side is inverse side at your Artist - Album association, i.e. changes at the 'albumList' are not handled by hibernate. You should you 'delete-orphan' facility instead - 10.11. Transitive persistence.

The following annotation should be added to the Artist.getAlbumList() metehod in your case:
Code:
    @OneToMany(mappedBy = "artist", cascade = CascadeType.ALL)
    @org.hibernate.annotations.Cascade(
        value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN
    )
    public List<Album> getAlbumList() {
        return albumList;
    }


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