Hibernate: 无法使用外键查询数据库表

huangapple go评论105阅读模式
英文:

Hibernate: Unable to query a database table using a Foreign Key

问题

我正在尝试执行以下的 Hibernate 查询。

  1. Query query = session.createSQLQuery("from Rating as rating where rating.organization.idorganization = :idorganization");

但我总是遇到以下错误。

  1. hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions 您的 SQL 语法错误;请检查与您的 MySQL 服务器版本相对应的手册,查看在第 1 行附近使用的正确语法。
  2. org.hibernate.exception.SQLGrammarException:无法提取 ResultSet

以下是我的 Rating bean。

  1. public class Rating implements java.io.Serializable {
  2. private Integer idrating;
  3. private Organization organization;
  4. private User user;
  5. private double rating;
  6. private Date dateCreated;
  7. private Date lastUpdated;
  8. public Rating() {
  9. }
  10. public Rating(Organization organization, User user, double rating) {
  11. this.organization = organization;
  12. this.user = user;
  13. this.rating = rating;
  14. }
  15. public Rating(Organization organization, User user, double rating, Date dateCreated, Date lastUpdated) {
  16. this.organization = organization;
  17. this.user = user;
  18. this.rating = rating;
  19. this.dateCreated = dateCreated;
  20. this.lastUpdated = lastUpdated;
  21. }
  22. public Integer getIdrating() {
  23. return this.idrating;
  24. }
  25. public void setIdrating(Integer idrating) {
  26. this.idrating = idrating;
  27. }
  28. public Organization getOrganization() {
  29. return this.organization;
  30. }
  31. public void setOrganization(Organization organization) {
  32. this.organization = organization;
  33. }
  34. public User getUser() {
  35. return this.user;
  36. }
  37. public void setUser(User user) {
  38. this.user = user;
  39. }
  40. public double getRating() {
  41. return this.rating;
  42. }
  43. public void setRating(double rating) {
  44. this.rating = rating;
  45. }
  46. public Date getDateCreated() {
  47. return this.dateCreated;
  48. }
  49. public void setDateCreated(Date dateCreated) {
  50. this.dateCreated = dateCreated;
  51. }
  52. public Date getLastUpdated() {
  53. return this.lastUpdated;
  54. }
  55. public void setLastUpdated(Date lastUpdated) {
  56. this.lastUpdated = lastUpdated;
  57. }
  58. }

以下是 Rating.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  4. <!-- Generated Sep 19, 2020 8:15:23 PM by Hibernate Tools 4.3.1 -->
  5. <hibernate-mapping>
  6. <class name="beans.Rating" table="rating" catalog="autocircle" optimistic-lock="version">
  7. <id name="idrating" type="java.lang.Integer">
  8. <column name="idrating" />
  9. <generator class="identity" />
  10. </id>
  11. <many-to-one name="organization" class="beans.Organization" fetch="select">
  12. <column name="idorganization" not-null="true" />
  13. </many-to-one>
  14. <many-to-one name="user" class="beans.User" fetch="select">
  15. <column name="iduser" not-null="true" />
  16. </many-to-one>
  17. <property name="rating" type="double">
  18. <column name="rating" precision="22" scale="0" not-null="true" />
  19. </property>
  20. <property name="dateCreated" type="timestamp">
  21. <column name="date_created" length="0" />
  22. </property>
  23. <property name="lastUpdated" type="timestamp">
  24. <column name="last_updated" length="0" />
  25. </property>
  26. </class>
  27. </hibernate-mapping>

为什么我会遇到这个错误呢?

英文:

I am trying to fire the following Hibernare quarry.

  1. Query query = session.createSQLQuery(&quot;from Rating as rating where rating.organization.idorganization = :idorganization&quot;);

I always ended up with the error

  1. hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#39;from Rating as rating where rating.organization.idorganization = 65&#39; at line 1
  2. org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Below is my Rating bean

  1. public class Rating implements java.io.Serializable {
  2. private Integer idrating;
  3. private Organization organization;
  4. private User user;
  5. private double rating;
  6. private Date dateCreated;
  7. private Date lastUpdated;
  8. public Rating() {
  9. }
  10. public Rating(Organization organization, User user, double rating) {
  11. this.organization = organization;
  12. this.user = user;
  13. this.rating = rating;
  14. }
  15. public Rating(Organization organization, User user, double rating, Date dateCreated, Date lastUpdated) {
  16. this.organization = organization;
  17. this.user = user;
  18. this.rating = rating;
  19. this.dateCreated = dateCreated;
  20. this.lastUpdated = lastUpdated;
  21. }
  22. public Integer getIdrating() {
  23. return this.idrating;
  24. }
  25. public void setIdrating(Integer idrating) {
  26. this.idrating = idrating;
  27. }
  28. public Organization getOrganization() {
  29. return this.organization;
  30. }
  31. public void setOrganization(Organization organization) {
  32. this.organization = organization;
  33. }
  34. public User getUser() {
  35. return this.user;
  36. }
  37. public void setUser(User user) {
  38. this.user = user;
  39. }
  40. public double getRating() {
  41. return this.rating;
  42. }
  43. public void setRating(double rating) {
  44. this.rating = rating;
  45. }
  46. public Date getDateCreated() {
  47. return this.dateCreated;
  48. }
  49. public void setDateCreated(Date dateCreated) {
  50. this.dateCreated = dateCreated;
  51. }
  52. public Date getLastUpdated() {
  53. return this.lastUpdated;
  54. }
  55. public void setLastUpdated(Date lastUpdated) {
  56. this.lastUpdated = lastUpdated;
  57. }
  58. }

Below is the Rating.hbm.xml

  1. &lt;?xml version=&quot;1.0&quot;?&gt;
  2. &lt;!DOCTYPE hibernate-mapping PUBLIC &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;
  3. &quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;
  4. &lt;!-- Generated Sep 19, 2020 8:15:23 PM by Hibernate Tools 4.3.1 --&gt;
  5. &lt;hibernate-mapping&gt;
  6. &lt;class name=&quot;beans.Rating&quot; table=&quot;rating&quot; catalog=&quot;autocircle&quot; optimistic-lock=&quot;version&quot;&gt;
  7. &lt;id name=&quot;idrating&quot; type=&quot;java.lang.Integer&quot;&gt;
  8. &lt;column name=&quot;idrating&quot; /&gt;
  9. &lt;generator class=&quot;identity&quot; /&gt;
  10. &lt;/id&gt;
  11. &lt;many-to-one name=&quot;organization&quot; class=&quot;beans.Organization&quot; fetch=&quot;select&quot;&gt;
  12. &lt;column name=&quot;idorganization&quot; not-null=&quot;true&quot; /&gt;
  13. &lt;/many-to-one&gt;
  14. &lt;many-to-one name=&quot;user&quot; class=&quot;beans.User&quot; fetch=&quot;select&quot;&gt;
  15. &lt;column name=&quot;iduser&quot; not-null=&quot;true&quot; /&gt;
  16. &lt;/many-to-one&gt;
  17. &lt;property name=&quot;rating&quot; type=&quot;double&quot;&gt;
  18. &lt;column name=&quot;rating&quot; precision=&quot;22&quot; scale=&quot;0&quot; not-null=&quot;true&quot; /&gt;
  19. &lt;/property&gt;
  20. &lt;property name=&quot;dateCreated&quot; type=&quot;timestamp&quot;&gt;
  21. &lt;column name=&quot;date_created&quot; length=&quot;0&quot; /&gt;
  22. &lt;/property&gt;
  23. &lt;property name=&quot;lastUpdated&quot; type=&quot;timestamp&quot;&gt;
  24. &lt;column name=&quot;last_updated&quot; length=&quot;0&quot; /&gt;
  25. &lt;/property&gt;
  26. &lt;/class&gt;
  27. &lt;/hibernate-mapping&gt;

Why am I getting this error?

答案1

得分: 1

因为您正在使用createSQLQuery方法,所以应该使用SQL语句。在这种情况下,应该是:

> "select * from Rating as rating where
> rating.organization.idorganization = :idorganization"

或者使用HQL方法和适当的HQL查询。

英文:

Since you are using createSQLQuery method, the SQL Statement should be used.
In this case, it should be

> "select * from Rating as rating where
> rating.organization.idorganization = :idorganization"

Or use the HQL Method and appropriate HQL Query

huangapple
  • 本文由 发表于 2020年9月30日 15:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64132442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定