Hi all,
I'm trying to create a Many-to-Many relationship and add a couple of fields
to it. I based my mapping on this post:
http://sieze.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/
(which
was very clear and helpful).
Summarizing the mapping on that blog (Product, Item and ProductItem), we'll
have the the code below. I reproduced that on my mapping and I was able to
add a Product X Item to it. However, when I try to add the same Item to a
second Product, I get the following error message:
*Server Error: org.hibernate.NonUniqueObjectException: a different
object with the same identifier value was already associated with the
session*
The strange thing is that it works just fine when I try to add a second Item
to the same product (so Products may have several Items, but an Item cannot
belong to several Products).
I played a little bit with the annotations and either I keep getting this
error message or the ProductItem references on Product and Item are not
updated correctly.
Does anyone knows what I'm doing wrong here? I'm really running out of
ideas.
Thanks,
Aldo
@Entity
@Table(name = "item")public class Item {
...
/*
* Here is the annotation to add in order to
* Hibernate to automatically insert and update
* ProducItems (if any)
*/
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item", cascade =
{CascadeType.PERSIST, CascadeType.MERGE})
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
public List<ProductItem> getProductItems() { return this.productItems; }
}
@Entity
@Table(name = "product")public class Product {
...
/*
* I just followed the same annotations on getProductItems()
*/
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product",
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
public List<ProductItem> getProductItems() {
return this.productItems;
}
}
@Entity
@Table(name = "product_item")
@AssociationOverrides({
@AssociationOverride(name = "pk.item", joinColumns = @JoinColumn(name
= "item_id")),
@AssociationOverride(name = "pk.product", joinColumns =
@JoinColumn(name = "product_id"))})public class ProductItem {
...
private ProductItemPk pk = new ProductItemPk();
@EmbeddedId
private ProductItemPk getPk() {
return pk;
}
}
@Embeddablepublic class ProductItemPk implements Serializable {
private Item item;
private Product product;
...
}
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.