Skip to main content

[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

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


Back to the top