@AttributeOverrides和@Embeddable不起作用

时间:2014-03-11 20:59:57

标签: java hibernate

这是来自 Hibernate Recipies 的书第3章第一个问题当我试图运行时我收到以下错误线程中的异常" main" org.hibernate.MappingException:实体映射中的重复列:com.fun.hibernate.auto.idgen.Order列:地址(应使用insert =&#34映射; false" update =" false&# 34)

这是代码

@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Table(name="ORDERS")
public class Order {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="orderSequence" )
@SequenceGenerator(name="orderSequence",sequenceName="ORDERSEQ")
private Long id;


private Contact weekdayContact;
private Contact holidayContact;
public Order() {

}


public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}

public Contact getWeekdayContact() {
    return weekdayContact;
}
public void setWeekdayContact(Contact weekdayContact) {
    this.weekdayContact = weekdayContact;
}

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="recipient",column=@Column(name ="HOLIDAY_RECIPIENT")),
    @AttributeOverride(name="phone",column=@Column(name ="HOLIDAY_PHONE")),
    @AttributeOverride(name="address",column=@Column(name ="HOLIDAY_ADDRESS"))
})
public Contact getHolidayContact() {
    return holidayContact;
}
public void setHolidayContact(Contact holidayContact) {
    this.holidayContact = holidayContact;
}
}

可嵌入对象

@Embeddable
public class Contact {

private String recipient;
private String phone;

private String address;
public Contact() {

}
@Column(name = "WEEKDAY_RECIPIENT")
public String getRecipient() {
    return recipient;
}
public void setRecipient(String recipient) {
    this.recipient = recipient;
}
@Column(name ="WEEKDAY_PHONE")
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
@Column(name ="WEEKDAY_ADDRESS")
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

任何想法如何解决这个问题

1 个答案:

答案 0 :(得分:0)

阅读上面引用的Hibernate Recipes一书,我可以很容易地达到你在问题中描述的那一点。

我无法创建您正在处理的相同环境,因此我必须阅读并解释本书关于按注释和外部配置文件(XML)进行映射的内容。

通过研究你的代码和书中的章节,我认为在你的嵌入式对象的映射中你可能有很多冗余,所以我认为这是完全(或接近它)的问题:

在配置文件中,本书说按标签创建手动映射,如下所示:

        <component name="weekdayContact" class="Contact">
            <property name="recipient" type="string" column="WEEKDAY_RECIPIENT" />
            <property name="phone" type="string" column="WEEKDAY_PHONE" />
            <property name="address" type="string" column="WEEKDAY_ADDRESS" />
        </component>
        <component name="holidayContact" class="Contact">
            <property name="recipient" type="string" column="HOLIDAY_RECIPIENT" />
            <property name="phone" type="string" column="HOLIDAY_PHONE" />
            <property name="address" type="string" column="HOLIDAY_ADDRESS" />
        </component>

在这之上,本书还说你需要通过注释创建映射:

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="recipient",column=@Column(name ="HOLIDAY_RECIPIENT")),
    @AttributeOverride(name="phone",column=@Column(name ="HOLIDAY_PHONE")),
    @AttributeOverride(name="address",column=@Column(name ="HOLIDAY_ADDRESS"))
})
public Contact getHolidayContact() {
    return holidayContact;
}

所以,我认为这里只发生了两件事:

  1. 您正在映射XML文件和类中的嵌入对象。在这种情况下,您应该只使用一种类型,最好是注释。

  2. 我可以看到您的代码与本书所述的代码不同,这可能导致失败。在这种情况下,你应该再读一遍你的整个代码,看看是否有任何差异,而不是书中的差异,然后纠正它就像在例子中写的一样。

  3. 就是这样,祝你好运!