2

I have the following model: An entity of type A has an association to many pairs of entities of types (B, C). Each B entity appears in at most one pair in A, but the same C entity can show in multiple pairs. For example:

A1 --> (B1, C1)
   --> (B2, C2)
   --> (B3, C1)

A2 --> (B1, C3)
   --> (B2, C4)
   --> (B3, C4)

This follows the semantics of a java.util.Map<B, C> stored in A. Further, each C only appears for one entity A, so the A to C relationship is OneToMany. I would like to persist such a Map, and a similar example appears in the Java EE6 doc. As far as I can tell, my code (below), is essentially identical to the example:

@Entity
public class A implements Serializable {
    @OneToMany(fetch= FetchType.EAGER)
    @JoinTable(name="A_BC_MAP",
            joinColumns=@JoinColumn(name="A_ID"),
            inverseJoinColumns=@JoinColumn(name="C_ID"))
    @MapKeyJoinColumn(name="B_ID")
    private Map<B, C> pinnedCourses = new HashMap<Course, ScheduleTerm>();
    ...
}

The database schema for A_BC_MAP emerges as expected, with a three-column join table (A_ID, B_ID, C_ID). However, the primary key constraint consists of the (A_ID, C_ID) pair. Thus, I cannot store more than one (map key) B in the database with the same (map value) C, as I can in the map.

Is this the expected behavior? For map semantics, I would expect the primary key to consist of (A_ID, B_ID). Am I doing something wrong?

I'm currently using EclipseLink 2.3.0.

1 Answers1

0

I assume you are using EclipseLink to define your DDL? You could always use your own DDL script.

Having duplicates in a OneToMany is unusual, but is probably possible with the map key, you could log a bug to have the DDL generate define the map key as the primary key instead of the target foreign key.

James
  • 17,965
  • 11
  • 91
  • 146
  • Yes, I'm using EclipseLink, and am hoping to avoid writing my own script just to fix this one item. Where would I best go about logging such a bug? – Jonathan Dautrich Nov 16 '12 at 03:36