-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: mapping a Map whose key is a domain object and values sets
PostPosted: Sun Aug 21, 2005 2:31 pm 
Newbie

Joined: Wed Aug 17, 2005 12:35 pm
Posts: 5
Is there a way to map (ehm :-)) a Map whose keys are one of my domain objects and the values are Set(s) of other domain objects?

I just can find examples of mapping hibernate types to hibernate types or elements (or composite elements) but I would like to map to association.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 21, 2005 5:00 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 6:15 pm
Posts: 28
It's the role of the join table, isnt't it ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 21, 2005 7:51 pm 
Newbie

Joined: Wed Aug 17, 2005 12:35 pm
Posts: 5
I don't understand exactly what you mean but this is not a simple one-to-many or many-to-one association.

It's a map association, suppose you have a map where the key is an object and the value is a list of objects:


Code:

class ClassA {

    private HashMap associatedClassB;

    ClassA() {

        HashMap associatedClassB = new HashMap();
        ClassC classC = new ClassC();

        this.getAssociatedClassB(classC).add(new ClassB());
        this.getAssociatedClassB(classC).add(new ClassB());
        ....
       
    }
    .....

    public Map getAssociatedClassB() {

         return this.associatedClassB;


   }


}



To my understanding the mapping xml could be


Code:
   <class
            name="ClassA"
            table="table_class_a"
        >
         

        <map
            name="associatedClassB"
            lazy="false"
            sort="unsorted"
            cascade="save-update"
        >

            <key  column="associated_class_b_id"/>
           

            <one-to-many
                  class="ClassB"
            />
           
            <map-key-many-to-many column="associated_class_b_key"
             class="ClassC"/>
   
         </map>
   

</class>


but this is not the case, I receive the following error:

Code:
The content of element type "map" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(map-key|composite-map-key|map-key-many-to-many|index|composite-index|index-many-to-many|index-many-to-any),(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".


that looks quite strange to me as it looks like all mandatory subelements are at their place...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:09 am 
Newbie

Joined: Wed Aug 17, 2005 12:35 pm
Posts: 5
Ok, I got to some (partial) conclusion and I report it here hoping ic can be of some help for someone:

my error was that I was looking for a many-to-many association for values, not a one-to-many! so My mapping would be:

Code:

<class name="ClassA">

...

<map name="associatedClassB" table="classc_classb_map">
    <!-- the foreign key column linking the a map table row to the class table row to where the map belongs -->
    <key  column="classa_id"/>

    <!-- The map key referencing the id of a ClassC instance -->
    <map-key-many-to-many column="classc_id" class="ClassC"/>

    <!-- The map value referencing the id of a ClassB instance (table row) -->
    <many-to-many class="ClassB" column="classb_id"/>

</map>

...

</class>



now the problem is that this mapping still maps a ClassC key with a SINGLE ClassB value because the generated table has classa_id and classc_id as primary keys while classb_id is allowed to be the same.

To have a ClassC -> "list of ClassB" mapping would be enough to have all three "_id" columns as primary keys... I wonder if Hibernate is able to map this... I would you express this in hte mapping file?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 1:29 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
<map-key-many-to-many .../> is not a valid subelement of <map>. I used <index-many-to-many .../> which worked and generated appropriate ddl.

Larry


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 1:23 pm 
Newbie

Joined: Fri Oct 14, 2005 8:37 am
Posts: 10
Location: University of Edinburgh
Actually, map-key-many-to-many is a valid sub-element of map. I have got:

Code:
  <class name="ClassA">
    <id type="long">
      <generator class="native"/>
    </id>

    <map name="associatedClassB" inverse="true" cascade="all,delete-orphan">
      <key column="classAId"/>
      <map-key-many-to-many class="ClassC"
        column="classCId" />
      <one-to-many class="ClassB"/>
    </map>
  </class>


My problem in this case is that both classAId and classBId are set as
separated foreign keys, rather than as a single (composite) foreign key.
Any ideas?

Rafael


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 1:25 pm 
Newbie

Joined: Fri Oct 14, 2005 8:37 am
Posts: 10
Location: University of Edinburgh
Actually, map-key-many-to-many is a valid sub-element of map. I have got:

Code:
  <class name="ClassA">
    <id type="long">
      <generator class="native"/>
    </id>

    <map name="associatedClassB" inverse="true" cascade="all,delete-orphan">
      <key column="classAId"/>
      <map-key-many-to-many class="ClassC"
        column="classCId" />
      <one-to-many class="ClassB"/>
    </map>
  </class>


My problem in this case is that both classAId and classBId are set as
separated foreign keys, rather than as a single (composite) foreign key.
Any ideas?

Rafael


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.