【问题标题】:Repeated column in mapping for entity exception实体异常映射中的重复列
【发布时间】: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();
}

【问题讨论】:

  • 您不能将同一列定义为多次
  • 我找不到重复的列。哪一个?

标签: java mysql hibernate


【解决方案1】:

您不能将同一列定义为多次。如果存在两列,则需要提及 insert = "false" 和 update = "false"

@Entity
@AssociationOverrides({ @AssociationOverride(name = "pk.user", joinColumns = @JoinColumn(name = "id")),
@AssociationOverride(name = "pk.role", joinColumns = @JoinColumn(name = "id", insertable = false, updatable = false)) })
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();
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多