Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
Code:
<hibernate-mapping package="project.vo">
<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" cascade="none" order-by="name asc" lazy="false" inverse="false">
<key column="groupID" />
<many-to-many class="project.vo.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" cascade="none" order-by="name asc" lazy="false" inverse="false">
<key column="userID" />
<many-to-many class="project.vo.Group" column="groupID" />
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Transaction tx = session.beginTransaction();
Query query = session.createQuery("select u from User u order by u.name asc");
Iterator results = query.iterate();
while (results.hasNext()) {
User u = (User) results.next();
System.out.println("User: " + u.getId() + " " + u.getName());
}
tx.commit();
Full stack trace of any exception that occurs:Code:
Jun 18, 2005 8:59:55 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 207, SQLState: 42S22
Jun 18, 2005 8:59:55 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Invalid column name 'name'.
Jun 18, 2005 8:59:55 PM org.hibernate.event.def.DefaultLoadEventListener onLoad
INFO: Error performing load command
org.hibernate.exception.SQLGrammarException: could not initialize a collection:
[project.vo.User.memberships#2]
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.forceInitialization(Ab
stractPersistentCollection.java:280)
at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(Persist
enceContext.java:796)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.ja
va:223)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersiste
r.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultL
oadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventList
ener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListen
er.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEven
tListener.java:167)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventList
ener.java:79)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:655)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:261)
at org.hibernate.type.EntityType.resolve(EntityType.java:286)
at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:207)
at org.hibernate.impl.IteratorImpl.postNext(IteratorImpl.java:92)
at org.hibernate.impl.IteratorImpl.<init>(IteratorImpl.java:57)
at org.hibernate.loader.hql.QueryLoader.iterate(QueryLoader.java:412)
at org.hibernate.hql.ast.QueryTranslatorImpl.iterate(QueryTranslatorImpl.java:2
81)
at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:935)
at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41)
at HibernateTest.main(HibernateTest.java:32)
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)
... 27 more
org.hibernate.exception.SQLGrammarException: could not initialize a collection:
[project.vo.User.memberships#2]
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.forceInitialization(Ab
stractPersistentCollection.java:280)
at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(Persist
enceContext.java:796)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.ja
va:223)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersiste
r.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultL
oadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventList
ener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListen
er.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEven
tListener.java:167)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventList
ener.java:79)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:655)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:261)
at org.hibernate.type.EntityType.resolve(EntityType.java:286)
at org.hibernate.type.EntityType.nullSafeGet(EntityType.java:207)
at org.hibernate.impl.IteratorImpl.postNext(IteratorImpl.java:92)
at org.hibernate.impl.IteratorImpl.<init>(IteratorImpl.java:57)
at org.hibernate.loader.hql.QueryLoader.iterate(QueryLoader.java:412)
at org.hibernate.hql.ast.QueryTranslatorImpl.iterate(QueryTranslatorImpl.java:2
81)
at org.hibernate.impl.SessionImpl.iterate(SessionImpl.java:935)
at org.hibernate.impl.QueryImpl.iterate(QueryImpl.java:41)
at HibernateTest.main(HibernateTest.java:32)
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)
... 27 more
Name and version of the database you are using: SQL Server 2000The generated SQL (show_sql=true):Code:
Hibernate: select user0_.id as col_0_0_ from MM_USERS user0_ order by user0_.nam
e asc
Hibernate: select user0_.id as id0_, user0_.name as name2_0_ from MM_USERS user0
_ where user0_.id=?
Hibernate: select membership0_.userID as userID1_, membership0_.groupID as group
ID1_, group1_.id as id0_, group1_.name as name0_0_ from MM_USERGROUPLINKS member
ship0_ inner join MM_GROUPS group1_ on membership0_.groupID=group1_.id where mem
bership0_.userID=? order by membership0_.name asc
As per the above, I'm just creating 2 classes, User and Group with many-to-many association between them. The backend database tables are MM_USERS, MM_GROUPS, and MM_USERGROUPLINKS.
I'm basically just trying to list all the users when I got the above errors.
Questions:
1) Why does Hibernate use 3 queries for a simple query like this?
2) Why does Hibernate apply the order by clause to the MM_USERGROUPLINKS table instead?
3) How should I correct my mappings so that this will work out?
Thanks in advance for any suggestions!