I am getting a strange error with trying to use Hibernate with MySQL. I have the following named query:
SELECT SUM(s.plotline.value), s.character FROM Story as s WHERE s.chapter.chapterID = ?1 GROUP BY s.character ORDER BY SUM(s.plotline.value)
This gets translated into the following SQL statement:
select sum(plotline1_.Value) as col_0_0_, story0_.CharacterID as col_1_0_, character2_.CharacterID as Characte1_5_,
character2_.AuthorID as AuthorID5_, character2_.BookID as BookID5_, character2_.CharacterName as Characte2_5_,
character2_.LastModified as LastModi3_5_
from fantasy.story story0_, fantasy.plotline plotline1_
inner join fantasy.character character2_ on story0_.CharacterID=character2_.CharacterID
where story0_.PlotlineID=plotline1_.PlotlineID and story0_.ChapterID= 4
group by story0_.CharacterID
order by count(plotline1_.Value)
When I execute this I get:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'story0_.CharacterID' in 'on clause'
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
com.mysql.jdbc.Util.getInstance(Util.java:382)
If I change the query to this and run it directly in SQL it runs properly:
select sum(plotline1_.Value) as col_0_0_, story0.CharacterID as col_1_0_, character2_.CharacterID as Characte1_5_,
character2_.AuthorID as AuthorID5_, character2_.BookID as BookID5_, character2_.CharacterName as Characte2_5_,
character2_.LastModified as LastModi3_5_
from fantasy.story story0, fantasy.plotline plotline1_
inner join fantasy.character character2_ on CharacterID = character2_.CharacterID
where story0.PlotlineID=plotline1_.PlotlineID and story0.ChapterID= 4
group by story0.CharacterID
order by count(plotline1_.Value)
There are two parts to this question
Your first request does not work because "story0_" is unknown in the "on clause"
from fantasy.story story0_, fantasy.plotline plotline1_ inner join fantasy.character character2_ on story0_.CharacterID=character2_.CharacterID
When you write "from A, B inner join C", SQL (version 5.0 and later) considers an inner jointure between B and C only and then only allows a "on clause" between these 2 tables.
2 solutions should work :
from ( fantasy.story story0_, fantasy.plotline plotline1_ ) inner join fantasy.character character2_ on story0_.CharacterID=character2_.CharacterID
from fantasy.plotline plotline1_, fantasy.story story0_ inner join fantasy.character character2_ on story0_.CharacterID=character2_.CharacterID
More details available at : https://bugs.mysql.com/bug.php?id=13551
I imagine that the solution you have found works because CharacterID is also a field in plotline1_
I am currently facing this problem with Hibernate, and even if i found why the SQL Statement raised an exception (and how to correct it), i don't know how to configure Hibernate to force it to generate one of theses 2 solutions.
If anyone knows how to do it with Hibernate, Thanks for your help !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.