Hibernate hbm.xmlファイルのいくつかのクラスをJPAアノテーションに移行しています。Hibernate/JPAでケースごとに@Embeddedプロパティからフィールドを除外する
埋め込み可能なクラスAddress
がいくつかの場所で使用されています。各場所では、Addressのプロパティの異なるサブセットが使用されます。この不自然な例では、のShippingAddressがAddress.countryを使用するが、billingAddressがないこと
(ゲッター/セッターは、簡潔にするために省略)
@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", [email protected](name="ship_addr"),
@AttributeOverride(name="city", [email protected](name="ship_city"),
@AttributeOverride(name="state", [email protected](name="ship_state"),
@AttributeOverride(name="zip", [email protected](name="ship_zip"),
@AttributeOverride(name="country", [email protected](name="ship_country")
})
Address shippingAddress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="address", [email protected](name="bill_addr"),
@AttributeOverride(name="city", [email protected](name="bill_city"),
@AttributeOverride(name="state", [email protected](name="bill_state"),
@AttributeOverride(name="zip", [email protected](name="bill_zip")
})
Address billingAddress;
}
注意。どちらもAddress.emailを使用していません。
Hibernateは、私が明示的に提供していないカラムに対して@Column
タグを推測しているという問題があります。
@Transient
をすべてのAddress
フィールドに追加しようとしましたが、@AttributeOverride
は@Transient
には表示されません。
これに対応する方法はありますか?
これは、私たちの組織全体で使用されている共通のモデルクラスであるという問題があります。私は振舞いを変えずにこのクラスをJPAに移行する方法を探しています。あるいは、30の異なるチームがコードを調整するように強制しました。 – qualidafial
これは、&at;列を使用してオプトインするのではなく、&at; Transientを使用してオプトアウトするJPAのスイッチに問題があることにも注意してください。この考えは、&at;列の注釈をどこにも必要とせずに、JPAを侵襲の少ないものにすることでした。これは私たちを苦しめ、この1つのクラスを従来のXML設定からJPAアノテーションに移行することを不可能にしました。 – qualidafial
モデルを変更できない場合は、解決策が表示されません。 使用されていないAddressのプロパティを無視するだけで、DBに空白のままにしておく必要があります。汚い解決策が、最も侵襲的ではありません。 –