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: