[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[List Home]
|
Re: [eclipselink-users] Setting both sides of many-to-many relationship causes double-insert
|
- From: Mike Kienenberger <mkienenb@xxxxxxxxx>
- Date: Wed, 17 Jun 2009 09:12:11 -0400
- Delivered-to: eclipselink-users@eclipse.org
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=Nq5rJ88T7kNjKmykW8pzGzLrzgiGS2VZqIxBJ1F1RlQ=; b=fi/0oERWHiOU2y8ZXjRZYWJOAzl8mJ5DPGJT8ai48Dp+t40+0t8nl74aXCbecRv+bt qANOpOj+W3um0deZphLF1L3QyWXjV90lO/sbq2h9dzUTyjXq6iWB+GT3ckvUy67/JJY+ HX8rZ4oAXfkKFMUNG3rZXcyUyLZZXB2/8+McU=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=ippwRAC/SXDwIvYfceIVa/t1Vp72F2Hwb1NO18cFRbl9xHk8NVbumICNKBcTuEa9zZ ba7on7iegeksZUfynFZkOE7AtOGUz6UVBZW+eO6PtIg2lAY0GmwK9xNyDF2af3yzQ0AJ VvK71V8A81KKYekqNgktO74aQVwsfOIBiQSew=
Thank you very much, Chris! I was hoping it was something simple.
My integration tests now pass with that change.
Regards,
Mike
On Wed, Jun 17, 2009 at 8:53 AM, christopher
delahunt<christopher.delahunt@xxxxxxxxxx> wrote:
> Hello Mike,
>
> One side needs to specify that it uses the mapping information from the
> other using "mappedBy" in the mapping. Otherwise, both are considered
> independent but using the same join table, resulting in the behavior you
> see.
>
> Ie:
> Class Service {
> @ManyToMany(targetEntity=Order.class, mappedBy="serviceList")
> List orderList = null;
> ...
>
>
> Best Regards,
> Chris
>
> Mike Kienenberger wrote:
>>
>> Hello.
>>
>> Before I take the time to provide my situation as a standalone
>> example, I decided I'd better ask what the expected behavior is. I'm
>> condensing things down and simplifying -- not my actual code.
>>
>> We have a many-to-many join table set up between service and order:
>>
>> @IdClass(ServiceOrderJoinPK.class)
>> @MappedSuperclass
>> @Table(name="service_order_join")
>> public class ServiceOrderJoin ... {
>> @Id // first half of primary key
>> @Column(name="service_id")
>> private Long serviceId = null;
>> @Id // second half of primary key
>> @Column(name="order_id")
>> private Long orderId = null;
>>
>> public class Order {
>> @ManyToMany(targetEntity=Service.class, fetch=FetchType.LAZY)
>> @JoinTable (
>> name="service_order",
>> joinColumns=@JoinColumn(name="order_id",
>> referencedColumnName="id"),
>> inverseJoinColumns=@JoinColumn(name="service_id",
>> referencedColumnName="id")
>> )
>> private List<Service> serviceList = null; // with standard accessors
>>
>> [...Same pattern for class Service...]
>>
>>
>> This is the pseudo-code to cause the behavior we're seeing under
>> EclipseLink 1.0.2:
>>
>> - create order object
>> - fetch service object from database
>> - order.addToServiceList(service)
>> - service.addToOrderList(order);
>> - commit
>>
>> And here's our problem: double-insert.
>>
>> INSERT INTO service_order_join (service_id, order_id)
>> VALUES (2, 1000)
>>
>> INSERT INTO service_order_join (order_id, service_id)
>> VALUES (1000, 2)
>>
>> [EL Warning]: 2009.06.16
>>
>> 20:20:35.437--UnitOfWork(100365669)--Thread(Thread[main,5,main])--Exception
>> [EclipseLink-4002] (Eclipse Persistence Services - 1.0.2 (Build
>> SNAPSHOT-20090505)):
>> org.eclipse.persistence.exceptions.DatabaseException
>> Internal Exception: org.h2.jdbc.JdbcSQLException: Unique index or
>> primary key violation: PRIMARY_KEY_A24 ON
>> SERVICE_ORDER_JOIN(SERVICE_ID, ORDER_ID); SQL
>> statement: INSERT INTO service_order (order_id, service_id)
>> VALUES (?, ?) [23001-111]
>>
>> Thanks for your time.
>>
>> Regards,
>> Mike
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>