0

次のようなエンティティ スタブがあります。

@Entity
public class Company extends AbstractEntity{

    private String name;
    private String address;
    private String zipCode;
    private String city;
    private String owner;
    private String website;
    private String emailAddress;
    private String phoneNumber;
    private String faxNumber;
    private List<Category> categories;
    private Map<Category, ServiceType> serviceType;

}

ご覧のとおり、ここでの目標は、serviceTypeキーが別のコレクション プロパティ (ここではプロパティはcategories) のメンバーであるマップ ( ) を作成することです。つまり、 stored ; を取得ServiceTypeしたいのです。別のマッピングされたエンティティです。注釈を使用してその目標を達成するにはどうすればよいですか? 使用するCategorycategoriesCategoryHibernate 4.2.1.Final

4

1 に答える 1

1

との間に結合テーブルがCompanyありCategory、 を指定する追加の列がある場合は、注釈ServiceTypeを使用してこれを実現できます。このブログ投稿@ElementCollectionを確認するか、Web を検索して他の例を探してください。基本的に、コレクションに対してorを使用してから、マップに対して次のように使用できます。@OneToMany@ManyToManycategories@ElementCollection

@ElementCollection
// company_id is the column that connects the company table to the join table
@CollectionTable(name = "company_category_servicetypes", joinColumns = @JoinColumn(name = "company_id", insertable=true, updatable=true))
// service_type is the "extra" information you want on the relation, basically the value in the map
@Column(name = "service_type", insertable=true, updatable=true)
// category_id is the other side of the join table (connecting to the category table)
@MapKeyJoinColumn(name = "category_id", insertable=true, updatable=true)
private Map<Category, ServiceType> serviceType;

categoriesこのアプローチを採用する場合は、リストを完全に削除することをお勧めします。同じ関係/結合テーブルが複数回マップされると、問題と混乱が生じる可能性があります。サービス タイプを無視して会社のカテゴリだけが必要なコードがある場合は、代わりにマップkeySetから取得できます。serviceType

于 2013-10-29T14:02:48.817 に答える