我目前正在将EclipseLink 2.5用于一个项目,并且在ManyToMany Relation中遇到一个小问题:

我有一个映射的超类Identifiable,它定义了一个复合键(uuid,修订版)。实体类AppointmentContent都是Identifiable的子类。

现在,我尝试定义从AppointmentContent的单向ManyToMany关系,但是EclipseLink似乎并没有正确创建JoinTable。其中只有两列(uuid,修订版),但是应该有四列(关系的每一侧都有一个uuid和修订版)。

一定但并非最不重要的是,代码:

@MappedSuperclass
@IdClass(IdentifiablePK.class)
public abstract class Identifiable implements Serializable {

    @Id
    @UuidGenerator(name = "uuid")
    @GeneratedValue(generator = "uuid")
    protected String uuid;

    @Id
    protected Integer revision;

    /* ... */
}


public class Appointment extends Identifiable {

    @JoinColumns({
        @JoinColumn(columnDefinition = "trainingcontent_uuid", referencedColumnName = "uuid", nullable = false, updatable = false, insertable = false),
        @JoinColumn(columnDefinition = "trainingcontent_revision", referencedColumnName = "revision", nullable = false, updatable = false, insertable = false)
    })
    @ManyToMany(fetch = FetchType.LAZY)
    protected List<TrainingContent> trainingContents;

    /* ... */
}


public class TrainingContent extends Identifiable {
    /* ... */
}


我也尝试使用@JoinTable代替@JoinColumns,但是EclipseLink抱怨缺少相应的@JoinColumns注释,这对于具有复合键的目标实体是必需的。

难道我做错了什么?

问候,
切尔特

最佳答案

对于ManyToMany,您必须对@JoinColumns使用@JoinTable,在@JoinTable内部指定源和目标连接列。

您的@JoinColumn一定不能包含“ updatable = false,insableable = false”,这没有任何意义,因为那样一来,它将无法插入任何东西,这似乎正在发生。

看到,
https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

10-05 17:53