-->
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.  [ 1 post ] 
Author Message
 Post subject: Many-to-many, join table w/ attribs, collection duplicated?
PostPosted: Mon Oct 08, 2007 5:30 am 
Newbie

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

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

I have the following:

@Entity
@Table(name = "establishments")
public class Establishment {
...
private Set<EstablishmentContainer> establishmentContainers;
...
@OneToMany(mappedBy="establishment", fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
public Set<EstablishmentContainer> getEstablishmentContainers ()
{
return establishmentContainers;
}

@Entity
@Table(name = "containers")
public class Container {
...
private Set<EstablishmentContainer> establishmentContainers;
...
@OneToMany(mappedBy="container")
public Set<EstablishmentContainer> getEstablishmentContainers ()
{
return establishmentContainers;
}

@Entity
public class EstablishmentContainer {
...
private Establishment establishment;
private Container container;
...
@ManyToOne
@JoinColumn(name="idEstablishment")
public Establishment getEstablishment()
{
return establishment;
}

@ManyToOne
@JoinColumn(name="idContainer")
public Container getContainer()
{
return container;
}

The user can add Containers from the establishment.jsp page. When I save, the collection is properly updated but Hibernate is not deleting the existing EstablishmentContainer objects. So if I start out with 2 and don't change anything, I now have 4! If I add one I have 5!

Do I have to somehow manually delete the existing EstablishmentContainers before saving? Just before calling establishment.save() I see that my Establishment object has the correct number of EstablishmentContainers. But when I save I have the exisiting EstablishmentContainers + any new ones I may have added or modified.

Can someone tell me how to prevent this?

Thanks!
Bob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.