【发布时间】:2014-11-02 19:19:48
【问题描述】:
我正在尝试执行以下操作:Many to Many Hibernate Mapping for additional property in the join table
用于创建带有额外字段的多对多连接。我不断收到此异常:
org.hibernate.MappingException: Repeated column in mapping for entity:
UserRole column: id (should be mapped with insert="false" update="false")
为什么?
我正在使用 spring 4 和 MySQL 我的代码:
@Entity
@AssociationOverrides({ @AssociationOverride(name = "pk.user", joinColumns = @JoinColumn(name = "id")),
@AssociationOverride(name = "pk.role", joinColumns = @JoinColumn(name = "id")) })
public class UserRole extends AbstractEntity {
private UserRoleId pk;
public UserRole(User user, Role role) {
super();
this.pk = new UserRoleId(user, role);
}
@EmbeddedId
public UserRoleId getPk() {
return pk;
}
public Long getUserId() {
return pk.getUser().getId();
}
public Long getRoleId() {
return pk.getRole().getId();
}
}
@Embeddable
public class UserRoleId implements Serializable {
private User user;
private Role role;
public UserRoleId(User user, Role role) {
super();
this.user = user;
this.role = role;
}
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
@MappedSuperclass
public abstract class AbstractEntity {
@Column(nullable = false)
protected Date timeCreated;
@Column
private Long modifiedBy;
public AbstractEntity() {
super();
this.timeCreated = DateUtil.now();
this.modifiedBy = 0l;
}
public Date getTimeCreated() {
return timeCreated;
}
public void setTimeCreated(Date timeCreated) {
this.timeCreated = timeCreated;
}
@Override
public abstract String toString();
}
【问题讨论】:
-
您不能将同一列定义为多次
-
我找不到重复的列。哪一个?