-->
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: OneToMany / ManyToOne update/delete collection not working
PostPosted: Thu Oct 25, 2007 5:12 am 
Newbie

Joined: Sat Jan 14, 2006 8:03 pm
Posts: 18
Hibernate version: 3.2.5.ga

Mapping documents:

@Entity
@Table(name = "places")
public class Place
{

private Set<PlaceCan> placeCans;

@OneToMany(mappedBy="place", fetch = FetchType.EAGER,
cascade = { CascadeType.ALL })
public Set<PlaceCan> getPlaceCans ();

@Entity
@Table(name = "cans")
public class Can
{

private Set<PlaceCan> placeCans;

@OneToMany(mappedBy="can", cascade = CascadeType.ALL)
public Set<PlaceCan> getPlaceCans ()

@Entity
public class PlaceCan
{

private Long id;
private Place place;
private Can can;
private int quantity;

@Column(name="idPlaceCan")
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId()

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idPlace")
public Place getPlace()

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idCan")
public Cubo getCan()




Name and version of the database you are using: mySql 5

Pretty typical mapping for this kind of situation -- an association table that has association info. In this case, every Place can have x number of Cans (garbage cans, that is ...), and the association info is 'quantity'.

When I save a Place object, deletes and updates are not persisted. I see updates to the 'placecan' table I can't see the actual values since Hibernate is using PreparedStatements, all I see are '?' where the values will be substituted.

At any rate, no changes are persisted. And if I delete, they just re-appear. I tried changing Place like this:

Code:
@OneToMany(mappedBy="place", fetch = FetchType.EAGER)
  @org.hibernate.annotations.Cascade(
  {org.hibernate.annotations.CascadeType.ALL,
  org.hibernate.annotations.CascadeType.DELETE_ORPHAN})    
  public Set<PlaceCan> getPlaceCans ()


... but with this code I don't even see update lines in the SQL log and if I delete I get this error:

Quote:
deleted object would be re-saved by cascade (remove deleted object from associations)


Can someone please tell me what I am doing wrong? I've been at this for days, no end in sight.

Thank you,
Bob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 3:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
My bet is that you don't update both sides of the relationship as you should.
When removing a can from a place, remove it from the collection and also do can.setPlace(null);

_________________
Emmanuel


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.