-->
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: One-to-Many can't remove non owning side
PostPosted: Fri Mar 17, 2006 5:35 pm 
Newbie

Joined: Fri Mar 17, 2006 4:48 pm
Posts: 3
Using
JBoss 4.0.4RC1
MySQL 5.0.18

I have 2 classes PlayList and Song. A PlayList has a List of Songs but a song doesn't know about a PlayList and a Song can be in only 1 playlist or not have a playlist at all. So we have a one-to-many unidirectional relationship.

Code:
@Entity
@Table(uniqueConstraints=
   {@UniqueConstraint(columnNames={"name"})})
public class PlayList {
   private Long id;
   private String name;
   private List<Song> songs;
   
   ...
   
   public void setId(Long id) {
      this.id = id;
   }
   @Id
   @GeneratedValue
   public Long getId() {
      return id;
   }
   
   public void setSongs(List<Song> songs) {
      this.songs = songs;
   }
   
        @OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE})   
   public List<Song> getSongs() {
      return songs;
   }

}


@Entity
@Table(uniqueConstraints=
{@UniqueConstraint(columnNames={"name"})})
public class Song {
   private Long id;
   private String name;
   ...
   public void setId(Long id) {
      this.id = id;
   }
   @Id
   @GeneratedValue
   public Long getId() {
      return id;
   }
}


When the schema is generated 3 tables result:
Code:
create table PlayList (id bigint not null auto_increment, name varchar(255), primary key (id), unique (name))
create table PlayList_Song (PlayList_id bigint not null, songs_id bigint not null, unique (songs_id))
create table Song (id bigint not null auto_increment, name varchar(255), primary key (id), unique (name))


The problem comes when I try to delete a song, which is associated with a playlist

Code:
entityManager.remove( aSong );


Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails ('music/playlist_song`, CONSTRAINT `FK1428A70D9548933E` FOREIGN KEY (`songs_id`) REFERENCES `song` (`id`))
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
   at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:517)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
   ... 57 more


How can I tell hibernate that it's ok to remove a song from the playlist_song table?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 20, 2006 4:55 pm 
Newbie

Joined: Fri Mar 17, 2006 4:48 pm
Posts: 3
I figured out a way around this problem. Before removing a song I go through the PlayList's songs and remove the song from there first.

Code:

Song pSong = entityManager.find(Song.class,songId);
           
List<PlayList> playlists = entityManager.createQuery("SELECT p FROM PlayList AS p").getResultList();
                       
for( PlayList p : playlists ) {               
      p.getSongs().remove(pSong);
}
entityManager.flush();
           
entityManager.remove(pSong);


This works and gets rid of the annoying exception. I still think that there should be a better way to do this but so far it's the best solution I came up with.

If anyone knows how to do this more cleanly I would appreciate it if you post it.


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.