[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [eclipselink-users] @ManyToOne and @JoinTable vs. @JoinColumns

Ben,

If there is a FK from T1 to T2 then I would expect T1 to have a ManyToOne relationship to T2 instead of a OneToMany, which by the way is intended to involve 3 tables when using a JoinTable. I would recommend setting up the ManyToOne on T1 targeting T2 and then you can setup the OneToMany on T2 using the first mapping in the mappedBy configuration.

Doug 

-----Original Message-----
From: Ben Horowitz [mailto:horowitz2@xxxxxxxx]
Sent: Wednesday, July 23, 2008 8:09 PM
To: eclipselink-users@xxxxxxxxxxx
Subject: [eclipselink-users] @ManyToOne and @JoinTable vs. @JoinColumns


Hi,

I have a database with composite primary keys, and am running into
issues when using @ManyToOne with @JoinTable that I don't run into when
using @ManyToOne with @JoinColumns.  I'm wondering if this is a mistake
on my part, or a bug.

Here's a description of the database:

- There are two tables involved, which I'll call T1 and T2.
- T1 has a composite primary key consisting of five columns, named, say,
C1 through C5.
- T2 has a composite primary key consisting of three columns, C1, C4,
and C5.
- There is a foreign key constraint between T1 and T2, that the columns
C1, C4, and C5 in T1 reference the columns of the same name in T2.  (In
SQL: ALTER TABLE T1 ADD CONSTRAINT FK_T1_T2 FOREIGN KEY (C1, C4, C5)
REFERENCES T2(C1, C4, C5).)

I realize that such a schema is well outside the norm of contemporary
usage (read: it's a legacy database), but I absolutely can't change the
schema.

Here's the Java code (excerpted):

@Entity
@IdClass(T1PK.class)
@Table(name = "T1")
public class T1 {
     @Id
     private String c1;

     @Id
     private String c2;

     @Id
     private String c3;

     @Id
     private String c4;

     @Id
     private String c5;

     // Variant 1, using @JoinTable
     @OneToMany(fetch = FetchType.EAGER)
     @JoinTable(name = "T2", joinColumns = {
         @JoinColumn(name = "C1", referencedColumnName = "C1", nullable = false, insertable = false, updatable = false),
         @JoinColumn(name = "C4", referencedColumnName = "C4", nullable = false, insertable = false, updatable = false),
         @JoinColumn(name = "C5", referencedColumnName = "C5", nullable = false, insertable = false, updatable = false)
     })
     private T2 t2;

     // Variant 2, using @JoinColumns
     @OneToMany(fetch = FetchType.EAGER)
     @JoinColumns({
         @JoinColumn(name = "C1", referencedColumnName = "C1", nullable = false, insertable = false, updatable = false),
         @JoinColumn(name = "C4", referencedColumnName = "C4", nullable = false, insertable = false, updatable = false),
         @JoinColumn(name = "C5", referencedColumnName = "C5", nullable = false, insertable = false, updatable = false)
     })
     private T2 t2;
}

@Entity
@IdClass(T2PK.class)
@Table(name = "T2")
public class T2 {
     @Id
     private String c1;

     @Id
     private String c4;

     @Id
     private String c5;
}

Variant 2 works fine.  Variant 1 results in error messages of the form
"Multiple writable mappings exist for the field [T1.c1]" (and for
T1.c4, and for T1.c5).

Is there a reason why @JoinColumns should work fine, but @JoinTable
should not?

I'd be glad to dig into this in more depth.  I just wanted to find out,
before I do, if I'm making a usage error.

Thanks a lot,
Ben Horowitz

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users