-->
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.  [ 6 posts ] 
Author Message
 Post subject: Please help!! Mapping a HashMap.
PostPosted: Mon Feb 16, 2004 5:25 pm 
Newbie

Joined: Sun Jan 25, 2004 2:35 pm
Posts: 5
Hello,

sorry, but this is my 3rd posting about this subject. I had tried some answers but it doesn't work.

I want to map the following class structure:

Code:
public class Template {

   public HashMap containers = new HashMap();

   public void setSingleContainers(Map singleContainers) {

      this.containers = singleContainers;
   }

   Map getSingleContainers() {

      return this.containers;
   }

   ...
}

public class SingleContainer {

   public String name = "";

   public void setName(String name) {

      this.name = name;
   }

   ...
}



Each instance of Template can contain 0...n different SingleContainers. Each SingleContainer can be member of 1 or more Templates. So I have the following table structure:

Template
======
idTemplate
name

Template_SingleContainer
=================
idTemplate
idSingleContainer

SingleContainer
==========
idSingleContainer
name

My mapping looks like this:

Code:
<class name="foo.bar.Template" table="Template">
   
      <id name="id" column="idTemplate">
         <generator class="increment"/>
      </id>
     
      <property name="name"/>
     
      <map name="SingleContainers" table="Template_SingleContainer">
         <index column="name" type="string"/>
         <key column="idTemplate"/>
         <many-to-many class="foo.bar.SingleContainer" 
            column="idSingleContainer"/>
      </map>

</class>
...


I need to use the name SingleContainer.name as key for the HashMap containers but I don't know how to do that. All what I got is this exception:

Caused by: java.sql.SQLException: Column not found, message from server: "Unknown column 'template0_.idSingleContainer' in 'field list'"

What's that: template0_ ?

Searching in the docs or in the forum doesn't help.

It would be nice If you can help me or give me a short example.

Thank you very much.

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 5:38 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
I haven't done any many-to-many mappings yet (will soon though!), but I do know one thing: your Template class needs to declare containers as a Map, *not* a HashMap. You can still do "new Template(new HashMap())" but Hibernate will be managing the actual contents of the containers field with its lazy containers, so you can't hardcode the class of Map that it is.

Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 5:43 pm 
Newbie

Joined: Sun Jan 25, 2004 2:35 pm
Posts: 5
RobJellinghaus wrote:
I haven't done any many-to-many mappings yet (will soon though!), but I do know one thing: your Template class needs to declare containers as a Map, *not* a HashMap. You can still do "new Template(new HashMap())" but Hibernate will be managing the actual contents of the containers field with its lazy containers, so you can't hardcode the class of Map that it is.

Cheers!
Rob


Yes, of course! It's an error of retyping into the form but it doesn't solve the problem ;-) The correct line looks like this:

Code:
Map containers = new HashMap();


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 7:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well, Unknown column 'template0_.idSingleContainer' means just that, there is no column idSingleContainer in your relation Table.

What you want to do is not possible AFAIK, the index column must be in the relation table, not in the table of the referenced object.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 3:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
gloeglm wrote:
What you want to do is not possible AFAIK, the index column must be in the relation table, not in the table of the referenced object.

Correct. Plus don't do public HashMap containers = new HashMap(); in your Template class, let it to null : It'll confuse Hibernate to have a pre setted Map.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 3:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
emmanuel wrote:
Correct. Plus don't do public HashMap containers = new HashMap(); in your Template class, let it to null : It'll confuse Hibernate to have a pre setted Map.


No, this is not a problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.