2015-02-06 55 views
0

讓我們假設我有以下3個表:Hibernate的地圖<基本,實體>映射(3臺)

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表映射到我的使用實體類,在地圖的值的類型是ENTITY3 。換句話說,我需要這樣的事情在我的 「ENTITY1」 休眠的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> 

< ??????????? ?? >是我無法找到的部分。

回答

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

希望這可以幫助你。

+0

是的,這正是我需要的。謝謝 – 2015-02-09 14:12:59