-->
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.  [ 10 posts ] 
Author Message
 Post subject: Many-to-many Map help.. frustrated beginner
PostPosted: Wed Aug 25, 2004 11:04 pm 
Newbie

Joined: Wed Aug 25, 2004 10:43 pm
Posts: 14
Location: NJ
I'm trying to map a many-to-many collection of Group objects into a Map on my User object, keyed by group name with values of Group object. Seems I can't get the mapping correct.

My table structure is like this:

USERS -> user_id
GROUP_MAP -> user_id, group_id (composite PK, both FKs)
GROUPS -> group_id

So, I have a table for users, and a table for groups, and they're mapped together using the group_map table.

I got it working fine as a set, using this mapping:
Code:
        <set name="groups" table="group_map" inverse="false" lazy="true">
            <key column="user_id"/>
            <many-to-many class="com.ezabel.core.user.Group" column="group_id"/>
        </set>


But, I'm unsuccessful getting it to work as a map. Again, I'd like the group.name column to be the key in the map, and the Group object the value.

Any idea what I'm doing wrong? Info posted below..

Thanks,
Ian.

Hibernate version:
Hibernate 2.1.6

Mapping documents:
Code:
<class name="com.ezabel.core.user.User" table="users">
        <id name="id" type="string" unsaved-value="null" column="user_id">
            <generator class="native"/>
        </id>

        <map name="groups" table="group_map" inverse="false" lazy="true">
            <key column="user_id"/>
            <index type="string" column="name"/>
            <many-to-many class="com.ezabel.core.user.Group" column="group_id"/>
        </map>
    </class>

    <class name="com.ezabel.core.user.Group" table="groups">
        <id name="id" type="int" column="group_id" unsaved-value="-1">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
        <property name="description" column="description" type="string"/>
        <property name="active" column="active" type="boolean"/>
    </class>


Code between sessionFactory.openSession() and session.close():
Code:
user = (User)session.load( User.class, userId );
Hibernate.initialize( user.getGroups() );


Full stack trace of any exception that occurs:

Code:
java.sql.SQLException: Column not found,  message from server: "Unknown column 'groups0_.name' in 'field list'"
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
   at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
   at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1555)
   at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
   at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
   at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
   at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
   at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990)

Name and version of the database you are using:
MySQL 4.0.16

Debug level Hibernate log excerpt:

Code:
Hibernate: select user0_.user_id as user_id0_ from users user0_ where user0_.user_id=?
Hibernate: select groups0_.user_id as user_id__, groups0_.group_id as group_id__, groups0_.name as name__, group1_.group_id as group_id0_, group1_.name as name0_, group1_.description as descript3_0_, group1_.active as active0_ from [b]group_map groups0_[/b] inner join groups group1_ on groups0_.group_id=group1_.group_id where groups0_.user_id=?
2004-08-25 22:58:17,703 WARN [net.sf.hibernate.util.JDBCExceptionReporter] - <SQL Error: 1054, SQLState: S0022>
2004-08-25 22:58:17,703 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] - <Column not found,  message from server: "Unknown column 'groups0_.name' in 'field list'">
2004-08-25 22:58:17,734 WARN [net.sf.hibernate.util.JDBCExceptionReporter] - <SQL Error: 1054, SQLState: S0022>
2004-08-25 22:58:17,734 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] - <Column not found,  message from server: "Unknown column 'groups0_.name' in 'field list'">
2004-08-25 22:58:17,734 ERROR [net.sf.hibernate.util.JDBCExceptionReporter] - <could not initialize collection: [com.ezabel.core.user.User.groups#iwz]>
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 11:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is not possible for a many-to-many association.

However, if you want to submit a feature rquest, I guess its something we could give some thought to.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 11:34 pm 
Newbie

Joined: Wed Aug 25, 2004 10:43 pm
Posts: 14
Location: NJ
gavin wrote:
This is not possible for a many-to-many association.

However, if you want to submit a feature rquest, I guess its something we could give some thought to.


Oh, haha, I hadn't expected that answer! Nice. Well, at least it wasn't something I was doing wrong, eh? ;)

How would I go about submitting a feature request?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 11:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
JIRA.

Not promising anything, by the way.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 5:16 pm 
Newbie

Joined: Tue Sep 14, 2004 5:01 pm
Posts: 2
Location: Baltimore, Maryland, USA
Gavin,

Can you unpack for me a little bit what you mean by "not possible for a many-to-many association"? I'm trying to do something similar, except that I want the Map key to be the same column as that specified in the many-to-many tag, as follows.

<class
name="Worker"
table="WORKER"

batch-size="1"
dynamic-insert="true"
dynamic-update="true"
lazy="false"
mutable="true"
optimistic-lock="version"
polymorphism="implicit"
>
<composite-id
access="property"
name="id"
class="WorkerID"
>
<key-property name="cossn" type="string" column="COSSN" />
<key-property name="cid" type="string" column="CID" />
</composite-id>

<timestamp
name="lastModTs"
column="LASTMOD_TS"
access="property"
unsaved-value="null"
/>


<map
name="employers"
table="WRKREMPLYR"
lazy="false"
cascade="all"
access="property"
>
<key>
<column name="COSSN" />
<column name="CID" />
</key>
<index column="EMPLOYER_ID" type="string"/>
<many-to-many class="Employer" column="EMPLOYER_ID" />
</map>

My database contains tables for WORKERS and EMPLOYERS. The many-to-many association between those two relations is realized by the table WRKREMPLYRS, which consists solely of the key columns of both tables.

When my app find()s a Worker, I need it to also retrieve all the Employers associated with that Worker, and place them in a java.util.Map named 'employers', which is a property of the Worker class. I need the Map to be indexed by the EMPLOYER_ID column, which is a UUID string generated by Hibernate and is the primary key of the EMPLOYERS table.

You see why I ask. Is this whole class of mapping objectives impossible in Hibernate, or is it more narrowly impossible to map a many-to-many association indexing on a value that's not the primary key of the collected entity?

Thanks in advance,
Steven


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 3:42 pm 
Newbie

Joined: Tue Apr 20, 2004 4:23 pm
Posts: 5
I've also just ran across the problem mentioned in the original message: need to map many-to-many assosiation with collection index field that's the same as many-to-many key column....

If there is a feature request I'd like to second that.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 4:40 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
I just posted such a feature request: http://opensource.atlassian.com/projects/hibernate/browse/HHH-250

See also my thread on a related issue: http://forum.hibernate.org/viewtopic.php?t=940128


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 31, 2008 12:13 am 
Newbie

Joined: Wed Jan 30, 2008 11:59 pm
Posts: 2
I am also looking to do this exact thing, but 3 1/2 years later. :)

I wish for my Map "members" in my Group class to have (type Long - FK from association table) memberId as a key and a Member class as value.

I have achieved this with a one-to-many Map, but this is a many-to-many. I see in the documentation there is a <map-key-many-to-many/> but it is hard for me to understand the usage of it.

Is this usage possible now in Hibernate 3.2.5?

Thanks,
Tom :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 5:02 am 
Newbie

Joined: Tue May 27, 2008 4:58 am
Posts: 1
same thing for me
I'm struggling to make it work.

Please someone can help?


Top
 Profile  
 
 Post subject: Re: Many-to-many Map help.. frustrated beginner [solution]
PostPosted: Wed Feb 17, 2010 5:49 pm 
Newbie

Joined: Wed Feb 17, 2010 5:43 pm
Posts: 1
Here is another solution using the formula attribute of <map-key>

In this example foo has a many-to-many relationship with bar via foo_has_bar.

Code:
foo          foo_has_bar     bar
foo_id <--    foo_id      --> bar_id
                  bar_id

<hibernate-mapping>
    <class name="Bar" table="bar" catalog="mydb">
        <id name="id" type="java.lang.Integer">
            <column name="bar_id" />
            <generator class="identity" />
        </id>
    </class>
    <class name="Foo" table="foo" catalog="mydb">
        <id name="id" type="java.lang.Integer">
            <column name="foo_id" />
            <generator class="identity" />
        </id>
        <map name="bars" inverse="true" cascade="all" table="foo_bar_x">
            <key column="foo_id" not-null="true" />
            <map-key type="int" formula="bar_id"/>
            <many-to-many column="bar_id" class="Bar"/>
        </map>
    </class>
</hibernate-mapping>



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