单向OneToMany Hibernate没有额外的连接表

时间:2016-06-29 19:15:36

标签: hibernate jpa one-to-many jointable

设置如下:我的应用程序有许多可能发生的事件。 Event只是一些添加的字段和一个包含一些信息的EventCode。基本上事件是一个连接表本身,添加了一些信息。

我想避免创建ApplicationId和EventId的表。只是似乎不需要

我得到的错误

Caused by: org.hibernate.MappingException: Foreign key (FK_157jxjblselu4urv8c0kqee6l:ApplicationEvent [applicationEvents_id])) must have same number of columns as the referenced primary key (ApplicationEvent [Application_id,applicationEvents_id])

申请(已删除很多,但这是问题所在)

@Entity
@Table
public class Application {
    //Lots of other stuff
    private Set<ApplicationEvent> applicationEvents = new LinkedHashSet<ApplicationEvent>();

    @OneToMany
    @JoinTable(name="ApplicationEvent")
    public Set<ApplicationEvent> getApplicationEvents() {
        return applicationEvents;
    }

    public void setApplicationEvents(Set<ApplicationEvent> applicationEvents) {
        this.applicationEvents = applicationEvents;
    }
}

活动

@Entity
@Table
public class ApplicationEvent {

    private Long id;
    private Long applicationId;
    private EventCode eventCode;
    private Boolean acknowledge;
    private int sequence;


    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column
    public Long getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(Long applicationId) {
        this.applicationId = applicationId;
    }

    @OneToOne
    @JoinColumn(name="EventCodeId")
    public EventCode getEventCode() {
        return eventCode;
    }

    public void setEventCode(EventCode eventCode) {
        this.eventCode = eventCode;
    }

    @Column
    public Boolean getAcknowledge() {
        return acknowledge;
    }

    public void setAcknowledge(Boolean acknowledge) {
        this.acknowledge = acknowledge;
    }

    @Column
    public int getSequence() {
        return sequence;
    }

    public void setSequence(int sequence) {
        this.sequence = sequence;
    }

//Some overriden methods
}

1 个答案:

答案 0 :(得分:1)

这种映射没有意义:您使用表ApplicationEvent作为Application和ApplicationEvent之间的连接表。

只需从applicationId删除ApplicationEntity(因为此列是实现OneToMany关联的一个,即连接列),然后使用

@OneToMany
@JoinColumn(name = "applicationId")