简体   繁体   English

Join On中的Hibernate MySQL查询错误未知列

[英]Hibernate MySQL Query Error Unknown Column in Join On

I am getting a strange error with trying to use Hibernate with MySQL. 尝试在MySQL中使用Hibernate时遇到一个奇怪的错误。 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: 它将转换为以下SQL语句:

    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: 如果将查询更改为此并直接在SQL中运行,它将正常运行:

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 这个问题有两个部分

  1. Why does removing the explicit qualification to CharacterID in the join make this work? 为什么要删除联接中对CharacterID的显式限定才能使此项工作? My experience has mainly been with using SQL Server and it would actually fail if you tried to write the query in this manner. 我的经验主要是使用SQL Server,如果尝试以这种方式编写查询,它实际上会失败。
  2. Is there a way to make Hibernate give me the query in the proper form or adjust my MySQL configuration so that it accepts the query the way Hibernate is producing it? 有没有办法让Hibernate以适当的形式给我查询或调整我的MySQL配置,以便它接受Hibernate生成查询的方式?

Your first request does not work because "story0_" is unknown in the "on clause" 您的第一个请求不起作用,因为“ on子句”中的“ story0_”未知

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. 当您编写“从A,B内部联接C”时,SQL(5.0及更高版本)仅考虑B和C之间的内部联接,然后仅在这两个表之间允许“ on子句”。

2 solutions should work : 2解决方案应该起作用:

  • Add parenthesis 添加括号

from ( fantasy.story story0_, fantasy.plotline plotline1_ ) inner join fantasy.character character2_ on story0_.CharacterID=character2_.CharacterID

  • Change table order 变更表格顺序

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 有关更多详细信息,请访问: 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_ 我想您找到的解决方案是有效的,因为CharacterID也是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. 我目前在使用Hibernate时遇到此问题,即使我发现了为什么SQL语句引发异常(以及如何更正它),我也不知道如何配置Hibernate来强制它生成这2种解决方案之一。

If anyone knows how to do it with Hibernate, Thanks for your help ! 如果有人知道如何使用Hibernate进行操作,谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM