逐案与Hibernate / JPA剔除案例@Embedded属性的字段

问题描述:

我在迁移一个Hibernate的hbm.xml文件JPA注解一些类。

I am migrating some classes in a Hibernate hbm.xml file to JPA annotations.

我们有一个在多个地方使用的嵌入式类地址。每个地方使用的地址的属性的不同的子集。

We have an embeddable class Address that is used in several places. Each place uses a different subset of the properties in Address.

(干将/不再赘述制定者)

(getters/setters omitted for brevity)

@Embeddable
public class Address {
  String email;
  String address;
  String city; 
  String state;
  String zip;
  String country;
}

@Entity
@Table(name="customer")
public class Customer {
  @Embedded
  @AttributeOverrides({
    @AttributeOverride(name="address", column=@Column(name="ship_addr"),
    @AttributeOverride(name="city", column=@Column(name="ship_city"),
    @AttributeOverride(name="state", column=@Column(name="ship_state"),
    @AttributeOverride(name="zip", column=@Column(name="ship_zip"),
    @AttributeOverride(name="country", column=@Column(name="ship_country")
  })
  Address shippingAddress;

  @Embedded
  @AttributeOverrides({
    @AttributeOverride(name="address", column=@Column(name="bill_addr"),
    @AttributeOverride(name="city", column=@Column(name="bill_city"),
    @AttributeOverride(name="state", column=@Column(name="bill_state"),
    @AttributeOverride(name="zip", column=@Column(name="bill_zip")
  })
  Address billingAddress;
}

请注意,在这种人为的例子,shippingAddress使用Address.country,但billingAddress没有;和他们都没有使用Address.email。

Note that in this contrived example, shippingAddress uses Address.country, but billingAddress does not; and neither of them use Address.email.

问题是,Hibernate是推断 @Column 标签,我没有明确规定之一的任何列。

The problem is that Hibernate is inferring @Column tags for any column where I haven't explicitly provided one.

我尝试添加 @Transient 所有地址字段,但似乎 @AttributeOverride 不王牌 @Transient

I tried adding @Transient to all the Address fields, but it appears that @AttributeOverride does not trump @Transient.

有没有办法解决这个?

我不认为这是可能的注释中嵌入的对象忽略从地址字段。

I don't think it is possible with annotations to "ignore" a field from address in your embedded objects.

一个解决办法是建立没有电子邮件基本类型地址和ExtendedAddress(地址的子类)与外地的电子邮件。

A workaround is to create a base type Address without email and an ExtendedAddress (subclass of Address) with the field email.