Hi!
I believe this should be somewhere in the FAQ but I didn't manage to find... Sorry.
The problem is: when I add order-by column to collection mapping hibernate assumes this column is in the join table. But I want to sort by column in the associated table itself. How to accomplish this?
Hibernate version: 3.0.2
Mapping documents:
Code:
<hibernate-mapping >
<class name="ua.spectruminfo.om.Product" table="products" >
<id name="id" column="id" access="field">
<generator class="assigned"/>
</id>
<property name="name" access="field"/>
<bag name="cats" access="field" order-by="ord asc" table="cats_products" >
<key column="product_id"/>
<many-to-many class="ua.spectruminfo.om.Category" column="cat_id" />
</bag>
</class>
<class name="ua.spectruminfo.om.Category" table="cats" >
<id name="id" column="id" access="field">
<generator class="assigned"/>
</id>
<property name="name" access="field" column="name" />
<property name="publish" access="field" type="boolean" column="publish" />
<property name="order" access="field" column="ord" />
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:Code:
org.hibernate.exception.SQLGrammarException: could not initialize a collection batch: [ua.spectruminfo.om.Product.cats#<D0646, S0201, S0204, S0028, S0112>]
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollectionBatch(Loader.java:1441)
at org.hibernate.loader.collection.BatchingCollectionInitializer.initialize(BatchingCollectionInitializer.java:47)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:483)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1354)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:171)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentBag.isEmpty(PersistentBag.java:226)
at ua.spectruminfo.om.Product.getSdfBlock(Product.java:314)
... 65 more
Caused by: java.sql.SQLException: Unknown column 'cats0_.ord' in 'order clause'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2376)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1860)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1705)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:75)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:118)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1233)
at org.hibernate.loader.Loader.doQuery(Loader.java:370)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
at org.hibernate.loader.Loader.loadCollectionBatch(Loader.java:1434)
... 73 more
Name and version of the database you are using: MySQL 4.0The generated SQL (show_sql=true):Code:
Hibernate: /*load collection ua.spectruminfo.om.Product.cats*/select cats0_.product_id as product1___, cats0_.cat_id as cat2___, category1_.id as id0_, category1_.name as name0_0_, category1_.publish as publish0_0_, category1_.ord as ord0_0_ from cats_products cats0_ inner join cats category1_ on cats0_.cat_id=category1_.id where cats0_.product_id in (?, ?, ?, ?, ?) order by cats0_.ord asc
[/code]