Hibernate map <basic, entity> 映射(3 表)

Hibernate map <basic, entity> mapping(3 tables)

假设我有这 3 个 table:

CREATE TABLE "entity1" (
    "id" int4 NOT NULL,
    "name" text NOT NULL,
    CONSTRAINT "entity1_pkey" PRIMARY KEY("id")
);

CREATE TABLE "entity2" (
    "id" int4 NOT NULL,
    "entity1_id" int4 NOT NULL,
    "param_name" text NOT NULL,
    "entity3_id" int 4 NOT NULL,
    CONSTRAINT "entity2_pkey" PRIMARY KEY("id")
    CONSTRAINT "fk_entity2_to_1" FOREIGN KEY "entity1_id" REFERENCE entity1("id")
    CONSTRAINT "fk_entity2_to_3" FOREIGN KEY "entity3_id" REFERENCE entity3("id")
);

CREATE TABLE "entity3" (
    "id" int4 NOT NULL,
    "name" text NOT NULL,
    CONSTRAINT "entity3_pkey" PRIMARY KEY("id")
);

我需要将 entity2 table 映射到我的 entity1 class,其中映射值的类型是 Entity3。换句话说,我的 "Entity1" Hibernate java bean 中需要这样的东西:

public class Holder {

    private Long                 id;

    private String               name;

    private Map<String, Entity3> data;

}

这是我目前所在的位置:

<hibernate-mapping package="test">

    <class name="Entity1" table="entity1">
        <id name="id" type="long" column="id">
            <generator class="native">
        </id>
        <property name="activated" column="activated" type="boolean" />

        <map name="data" table="entity2" cascade="all">
            <key column="entity1_id" />
            <map-key column="param_name" type="java.lang.String" />
            < ??????????????????????????? >
        </map>

    </class>

</hibernate-mapping>

< ?????????????? > 是我找不到的部分。

<???????> -> <many-to-many column="entity3-id" class="entity3" />

希望对您有所帮助。

你的问题答案是多对多映射见下文link对你有帮助

Many to many mapping in hibernate