-->
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: Invalid SQL generated with @OrderBy on @ManyToMany relation
PostPosted: Mon Mar 03, 2014 10:03 pm 
Newbie

Joined: Mon Mar 03, 2014 7:02 pm
Posts: 2
In my JavaEE web application, I have two entity beans that have a many-to-many relation. To use a simplified example let's have a secret agent, ("Agent"), that is assigned to a list of cities, ("City").

The relationship between the "Agent" and "City" entity is defined like this:
Code:
@ManyToMany(targetEntity = City.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(name = "Agent_City", joinColumns = @JoinColumn(name = "_agentId"), inverseJoinColumns = @JoinColumn(name = "_cityId"))
@OrderBy("cityName")
public List<City> getAssignedCities() {
     return assignedCities;
}


This deployed and worked fine in various versions of JBoss-7 and pre-GA versions of WildFly, up and until wildfly-8.0.0.Beta2, which was bundled with Hibernate 4.0.3.Beta4.

In my code, I fetch an "Agent" entity with a simple query-by-ID like this:

Code:
Agent agent = em.find(Agent.class, 1L);


In the GA release of WildFly, hovever, this now breaks, because the bundled Hibernate 4.1.3.Final generates invalid SQL when querying my "Agent" entities:

Code:
select agent0_.agentId as agentId1_0_0_, agent0_.agentName as agentNam2_0_0_,
        assignedci1_._agentId as _agentId1_0_1_, city2_.cityId as _cityId2_1_1_,
        city2_.cityId as cityId1_2_2_, city2_.cityName as cityName2_2_2_
        from Agent agent0_
        left outer join Agent_City assignedci1_ on agent0_.agentId=assignedci1_._agentId
        left outer join City city2_ on assignedci1_._cityId=city2_.cityId
        where agent0_.agentId=? order by assignedci1_.cityName


The "order by assignedci1_.cityName" uses the join table as the table reference for the "cityName" field instead of the City table. There is no "cityName" on the join table.

I have produced a minimal test case with the above entities that breaks when deployed using any Hibernate version after 4.0.3.Beta4. I even tried the most recent 4.3.4.Final to no avail; breaks with the same error. The test case can be found on GitHub, along with a detailed README file on how to reproduce it: https://github.com/smaragd/order-test

Is the way I use "@OrderBy" with "@ManyToMany" no longer supported? Any insight would be appreciated.


Top
 Profile  
 
 Post subject: Re: Invalid SQL generated with @OrderBy on @ManyToMany relation
PostPosted: Tue Mar 04, 2014 11:25 am 
Newbie

Joined: Tue Feb 25, 2014 12:02 pm
Posts: 3
I'm having the same problem too as posted here: https://forum.hibernate.org/viewtopic.php?f=1&t=1031237. Unfortunately I got no responses, so for the time being I've removed @OrderBy from any @ManyToMany relationship. If I need to so some sorting, then I do it on the @PostLoad.

EDIT: Looks like it's been logged since January: https://hibernate.atlassian.net/browse/HHH-8834


Top
 Profile  
 
 Post subject: Re: Invalid SQL generated with @OrderBy on @ManyToMany relation
PostPosted: Tue Mar 04, 2014 12:22 pm 
Newbie

Joined: Mon Mar 03, 2014 7:02 pm
Posts: 2
Nice find, Cain05. I have searched the Hibernate issue tracker and forum before my post, but only came up with references to older problems around @OrderBy from 2012.
HHH-8834 seems to exactly cover my problem here as well. Thanks for pointing it out.

EDIT: I added my vote to HHH-8834 and the link to my test case. Maybe someone form the team picks it up. :-)


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.