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.