Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
[code<class name="Group" table="MM_GROUPS">
<id name="id" column="id" type="int" unsaved-value="null" length="5">
<generator class="native" />
</id>
<property name="name" column="name" type="string" length="50" not-null="true" unique="false" />
<set name="members" table="MM_USERGROUPLINKS" inverse="false" order-by="name asc">
<key column="groupID" />
<many-to-many class="User" column="userID" />
</set>
</class>
<class name="User" table="MM_USERS" >
<id name="id" column="id" type="int" unsaved-value="null"
length="5">
<generator class="native" />
</id>
<property name="name" column="name" type="string" length="50" not-null="true" unique="false" />
<set name="memberships" table="MM_USERGROUPLINKS" inverse="false">
<key column="userID" />
<many-to-many class="Group" column="groupID" />
</set>
</class>[/code]
Code between sessionFactory.openSession() and session.close():
Code:
Query query = hbmSession.createQuery("select g from Group g");
results = query.list();
Iterator iter = results.iterator();
while (iter.hasNext()) {
group = (Group) iter.next();
System.out.println("name: " + group.getName());
Set members = group.getMembers();
Iterator mIter = members.iterator();
while (mIter.hasNext()) {
User u = (User) mIter.next();
System.out.println(u.getId() + " " + u.getName());
}
}
Full stack trace of any exception that occurs:Code:
Jun 19, 2005 11:05:27 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 207, SQLState: 42S22
Jun 19, 2005 11:05:27 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Invalid column name 'name'.
org.hibernate.exception.SQLGrammarException: could not initialize a collection:
[project.vo.Group.members#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java
:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1441)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader
.java:99)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(Ab
stractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializ
eCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPer
sistentCollection.java:176)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersisten
tCollection.java:48)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:134)
at HibernateTest.main(HibernateTest.java:50)
Caused by: java.sql.SQLException: Invalid column name 'name'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:367
)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2606)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2048)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:574)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:3
21)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedSta
tement.java:667)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
at org.hibernate.loader.Loader.doQuery(Loader.java:391)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.ja
va:218)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
... 8 more
Name and version of the database you are using: SQL Server 2000The generated SQL (show_sql=true):Code:
Hibernate: select group0_.id as id, group0_.name as name0_ from MM_GROUPS group0
_
name: POSC
Hibernate: select members0_.groupID as groupID1_, members0_.userID as userID1_,
user1_.id as id0_, user1_.name as name2_0_ from MM_USERGROUPLINKS members0_ inne
r join MM_USERS user1_ on members0_.userID=user1_.id where members0_.groupID=? o
rder by members0_.name asc
As above, I have a many-to-many mapping between User and Group (one user can belong to many groups, and one groups can have many users). The many-to-many association is stored in MM_USERGROUPLINKS table.
What I want to achieve is for the "members" Set in Group to be ordered by the user's names whenever it is retrieved. That's why I used the
order-by="name asc" attribute in the set mapping for members in Group.
However, the problem is that the set maps to MM_USERLINKS table, instead of MM_USERS table, and therefore Hibernate complains that it can't find the name field in MM_USERLINKS table.
In one-to-many associations, this has no problems at all, because the set maps to the actual table of the class, instead of to a bridging table.
Are there any way to go around this, short of using a native query?
Thanks for any advice!