Solution to Hibernate error: Unknown column ‘******’ in ‘order clause’

Bug: A above exception thrown when using hibernate.

Reason: This error occurs usually because of a misplaced “order by” clause.
If in a many to many mapping, the serial order is defined in the cross reference table, the order-by attribute should be provided in the “collection” node as below –

<bag name="projects" ... table="users_projects" inverse="true" order-by="sl_num">
  		<key column="userId"/>
  		<many-to-many column="projectId" class="Project"/>
 </bag>

On other hand, if the collection is to be sorted by a column defined in the collection member, the order-by should be provided in the many-to-many or one-to-many node, as shown below.

<bag name="projects" ... table="users_projects" inverse="true">
  		<key column="userId"/>
  		<many-to-many column="projectId" class="Project"  order-by="name"/>
 </bag>

Solution: Move the order-by attribute to appropriate node depending on which table holds the sorted field.

Sreekumar Kj
Sreekumar (KJ) has been a hobby programmer from school days. Codemarvels is his personal blog from the year 2010, where he writes about technology, philosophy, society and a bit about physics. He now runs a conversational AI company - DheeYantra - focusing his efforts to help businesses improve operational efficiency using digital employees powered by AI.