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

The base issue here is with your mappings. This relationship should be a X to One relationship because you do not have a collection. JoinTable should only be used if you actually have an intermediary table used to join two separate tables which does not seem to be the case here. JoinColumns is the way to go and either a ManyToOne mapping or a OneToOneMapping.
--Gordon


Ben Horowitz wrote:
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