如何在hibernate中重复嵌入式

时间:2011-09-09 12:12:30

标签: java hibernate

我正在尝试在Hibernate 3.6.x中映射这个类

@Entity @Table(name = "address") @Inheritance()
public abstract class Address {
    @Column(name = "address_type") @Enumerated(EnumType.STRING)
    private final AddressType addressType;

    @Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line1"))})
    private final AddressLine addressLine1;

    @Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line2"))})
    private final AddressLine addressLine2;

    @Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line3"))})
    private final AddressLine addressLine3;

    @Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line4"))})
    private final AddressLine addressLine4;

    @Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line5"))})
    private final AddressLine addressLine5;

    @Embedded @AttributeOverrides({@AttributeOverride(name = "postcode", column = @Column(name = "postcode"))})
    private final Postcode postcode;
}

我收到了这个错误。 (我不完全理解 - 有人可以解释一下吗?)

  

实体映射中的重复列:   地址栏:addressLine(应该   用insert =“false”update =“false”)

进行映射

虽然@Embeddable AddressLine只是一个包装字符串的对象,但我希望保留该类型,以便稍后我可以添加行为/规则。

任何可以允许此类配置的建议吗?

2 个答案:

答案 0 :(得分:1)

我没有使用@AttributeOverride,但我知道这条错误消息。它是由具有相同名称的列的多个映射引起的,在这种情况下意味着Hibernate忽略了列覆盖定义。

但是,就我而言,我使用的是@ManyToOne类,因此解决方案是@JoinColumns定义覆盖了原始列名。

答案 1 :(得分:1)

我很惭愧地说,我从这个问题中得到的错误是由于副本&粘贴犯罪。

虽然不在问题代码中,但在我的实际代码库中,重复的AddressLines 1,2,3和4中的@Column注释名称值未更改为唯一。

例如 - 这是 BAD ,因为@Column名称值与“address_line1”相同

@Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line1"))})
private final AddressLine addressLine1;

@Embedded @AttributeOverrides({@AttributeOverride(name = "addressLine", column = @Column(name = "address_line1"))})
private final AddressLine addressLine2;

如果所有列名都是唯一的,则此示例将按预期工作。