我在映射不同实体类的多个@EmbeddedId
列时遇到问题。我有一个像下面这样的实体结构(为了便于阅读,简化了实体)。
@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();
}
此实体的@EmbeddedId
定义如下:
@Embeddable
private class ActionId implements Serializable {
@ManyToOne private Operation operation;
private HttpMethod method;
}
pk列之间的映射工作正常。当我尝试将另一个实体作为链接表添加到Action
实体时出现问题。此链接表还有@EmbeddedId
。
@Entity
@AssociationOverrides .. ?
public class UserAction {
@EmbeddedId private UserActionId pk = new UserActionId();
}
@EmbeddedId
类看起来像:
@Embeddable
private class UserActionId implements Serializable {
@ManyToOne private Action action;
@ManyToOne private User user;
}
如何引用Action类的@EmbeddedId
字段,但只有一列是连接列。 ActionId
类中的另一列是常规@Column
。
我试过这个:
@AssociationOverrides({
@AssociationOverride(name = "pk.action.pk.operation", joinColumns = @JoinColumn(name = "id_operation", nullable = false)) })
但是这给了我一个错误:
引起:org.hibernate.MappingException:外键(FK_gwvslpytxm695kdw5lxqneyss:user_action [useraction]))必须与引用的主键具有相同的列数(useraction [id_operation,http_method]) 在org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:110) 在org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:93) 在org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1816) 在org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739) 在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl $ 4.perform(EntityManagerFactoryBuilderImpl.java:850)
如何覆盖UserAction实体中method
类的其他非连接列ActionId
关联?