【问题标题】:Hibernate: Repeated column in mapping for entity | two embedded classes of the same typeHibernate:实体映射中的重复列 |两个相同类型的嵌入类
【发布时间】:2011-11-07 18:32:52
【问题描述】:

Hibernate 在创建表时抛出异常,因为嵌入的属性映射到同一列。

类Distance在类路由中嵌入两次,如下所示:

@Embeddable
public class Distance implements Serializable{
    private static final long serialVersionUID = -8466495790824502626L;
    @Column(nullable = false)
    protected Integer distInSec;

    public Distance() {
        super();
    }
}


@Entity
public class Route{
    @Column(nullable=false)
    protected Distance currentDetour;

    @Column(nullable=false)
    protected Distance currentDist;       
}

当 hibernate 创建表时,它会尝试将 currentDetour 和 currentDist 的 distInSec 映射到表路由中的同一列“distInSec”。因此,错误 org.hibernate.MappingException: Repeated column in mapping for entity: 被抛出。

如果可能,我想更改配置,使其始终生成名称为 currentDetour_distInSec 和 currentDist_distInSec 的列。有人知道怎么做吗?

提前致谢

【问题讨论】:

    标签: hibernate jpa hibernate-mapping


    【解决方案1】:

    使用@AttributeOverride 自定义列名。

    【讨论】:

    • 这可行,但不是默认配置。还是谢谢你!
    【解决方案2】:

    使用 DefaultComponentSafeNamingStrategy 作为命名策略解决了这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 2020-06-22
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多