1

新しい PK クラス (つまり、@EmbeddedId) を作成せずに、アノテーションを使用して Hibernate で複合キーを作成する方法はありますか?

私の問題は、多くの属性を持つ抽象クラス CommonClass があり、それを多くのエンティティ クラスに継承する必要があることです。各クラスには異なるタイプの id がありますが、それらはすべて CommonClass にある属性を持つ複合キーである必要があります。例:

@MappedSuperclass
abstract class CommonClass {
    @Id
    int typed;

    int a0;
    int a1;
    //many other attributes
}

@Entity
class EntityString extends CommonClass {
    @Id
    String id;
    //ID need to be id+typed from CommonClass

    //other attributes
}

@Entity
class EntityInteger extends CommonClass {
    @Id
    Integer id;
    //ID need to be id+typed from CommonClass

    //other attributes
}

それで、これを行う最善の方法は何ですか?

4

1 に答える 1

2

次の hibernate docのセクション 2.2.3.2.2 。

おそらくより自然なもう 1 つのアプローチは、エンティティの複数のプロパティに @Id を配置することです。このアプローチは Hibernate でのみサポートされていますが、追加の埋め込み可能なコンポーネントは必要ありません。

@Entity
class Customer implements Serializable {
  @Id @OneToOne
  @JoinColumns({
    @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
    @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
  })
  User user;

  @Id String customerNumber;

  boolean preferredCustomer;
}

@Entity 
class User {
  @EmbeddedId UserId id;
  Integer age;
}

@Embeddable
class UserId implements Serializable {
  String firstName;
  String lastName;
}
于 2012-07-13T18:40:48.307 に答える