-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many with join table and order-by
PostPosted: Thu May 05, 2005 7:12 pm 
Newbie

Joined: Thu May 05, 2005 6:35 pm
Posts: 3
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.0

The 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]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 4:38 pm 
Newbie

Joined: Tue Nov 30, 2004 10:39 am
Posts: 10
I think this is not supported. See
http://forum.hibernate.org/viewtopic.ph ... ny+orderby


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 4:51 pm 
Newbie

Joined: Tue Nov 30, 2004 10:39 am
Posts: 10
forgot to tell that I solved such a problem with an in-memory sorting like:

Code:
<set name="mySet"
      table="MY_TABLE"
      cascade="all-delete-orphan"
      sort="de.mycomp.MyComparator">


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