Hibernate支持多个@EmbeddedId?

时间:2015-09-28 21:07:11

标签: java spring hibernate hibernate-4.x

是否可以为类之间的@ManyToOne关系定义多个@EmbeddedId复合键?例如:

实体1

@Entity
@AttributeOverrides({ @AttributeOverride(name="pk.method", column=@Column(name = "http_method", nullable = false)) })
@AssociationOverrides({ @AssociationOverride(name = "pk.operation", joinColumns = @JoinColumn(name = "id_operation", nullable = false)) })
public class Action {

    @EmbeddedId
    private ActionId pk = new ActionId();


}

@Embeddable
private class ActionId implements Serializable {

    @ManyToOne private Operation operation;
    private HttpMethod method;

}

实体2:

@Entity
@AssociationOverrides
// Need to override the embedded if attributes of the Action entity inside the UserActionId class
public class UserAction {

    @EmbeddedId private UserActionId pk = new UserActionId();
}

@Embeddable
private class UserActionId implements Serializable {

    // How to provide a @JoinColumn for the Action.pk composite key (action.pk.authURL) and @Column for (action.pk.httpMethod)
    @ManyToOne private Action action;
    @ManyToOne private User user;


}

这可能吗?我已经尝试过,但仍然遇到Hibernate错误,说FK必须与ActionId pk具有相同的列数。

0 个答案:

没有答案