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

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

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

问题

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

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

但我总是遇到以下错误。

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

以下是我的 Rating bean。

public class Rating implements java.io.Serializable {

     private Integer idrating;
     private Organization organization;
     private User user;
     private double rating;
     private Date dateCreated;
     private Date lastUpdated;

    public Rating() {
    }

    public Rating(Organization organization, User user, double rating) {
        this.organization = organization;
        this.user = user;
        this.rating = rating;
    }

    public Rating(Organization organization, User user, double rating, Date dateCreated, Date lastUpdated) {
       this.organization = organization;
       this.user = user;
       this.rating = rating;
       this.dateCreated = dateCreated;
       this.lastUpdated = lastUpdated;
    }

    public Integer getIdrating() {
        return this.idrating;
    }
    
    public void setIdrating(Integer idrating) {
        this.idrating = idrating;
    }
    public Organization getOrganization() {
        return this.organization;
    }
    
    public void setOrganization(Organization organization) {
        this.organization = organization;
    }
    public User getUser() {
        return this.user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }
    public double getRating() {
        return this.rating;
    }
    
    public void setRating(double rating) {
        this.rating = rating;
    }
    public Date getDateCreated() {
        return this.dateCreated;
    }
    
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Date getLastUpdated() {
        return this.lastUpdated;
    }
    
    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
}

以下是 Rating.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 19, 2020 8:15:23 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="beans.Rating" table="rating" catalog="autocircle" optimistic-lock="version">
        <id name="idrating" type="java.lang.Integer">
            <column name="idrating" />
            <generator class="identity" />
        </id>
        <many-to-one name="organization" class="beans.Organization" fetch="select">
            <column name="idorganization" not-null="true" />
        </many-to-one>
        <many-to-one name="user" class="beans.User" fetch="select">
            <column name="iduser" not-null="true" />
        </many-to-one>
        <property name="rating" type="double">
            <column name="rating" precision="22" scale="0" not-null="true" />
        </property>
        <property name="dateCreated" type="timestamp">
            <column name="date_created" length="0" />
        </property>
        <property name="lastUpdated" type="timestamp">
            <column name="last_updated" length="0" />
        </property>
    </class>
</hibernate-mapping>

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

英文:

I am trying to fire the following Hibernare quarry.

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

I always ended up with the error

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
org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Below is my Rating bean

public class Rating  implements java.io.Serializable {
private Integer idrating;
private Organization organization;
private User user;
private double rating;
private Date dateCreated;
private Date lastUpdated;
public Rating() {
}
public Rating(Organization organization, User user, double rating) {
this.organization = organization;
this.user = user;
this.rating = rating;
}
public Rating(Organization organization, User user, double rating, Date dateCreated, Date lastUpdated) {
this.organization = organization;
this.user = user;
this.rating = rating;
this.dateCreated = dateCreated;
this.lastUpdated = lastUpdated;
}
public Integer getIdrating() {
return this.idrating;
}
public void setIdrating(Integer idrating) {
this.idrating = idrating;
}
public Organization getOrganization() {
return this.organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public double getRating() {
return this.rating;
}
public void setRating(double rating) {
this.rating = rating;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getLastUpdated() {
return this.lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
}

Below is the Rating.hbm.xml

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;
&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;
&lt;!-- Generated Sep 19, 2020 8:15:23 PM by Hibernate Tools 4.3.1 --&gt;
&lt;hibernate-mapping&gt;
&lt;class name=&quot;beans.Rating&quot; table=&quot;rating&quot; catalog=&quot;autocircle&quot; optimistic-lock=&quot;version&quot;&gt;
&lt;id name=&quot;idrating&quot; type=&quot;java.lang.Integer&quot;&gt;
&lt;column name=&quot;idrating&quot; /&gt;
&lt;generator class=&quot;identity&quot; /&gt;
&lt;/id&gt;
&lt;many-to-one name=&quot;organization&quot; class=&quot;beans.Organization&quot; fetch=&quot;select&quot;&gt;
&lt;column name=&quot;idorganization&quot; not-null=&quot;true&quot; /&gt;
&lt;/many-to-one&gt;
&lt;many-to-one name=&quot;user&quot; class=&quot;beans.User&quot; fetch=&quot;select&quot;&gt;
&lt;column name=&quot;iduser&quot; not-null=&quot;true&quot; /&gt;
&lt;/many-to-one&gt;
&lt;property name=&quot;rating&quot; type=&quot;double&quot;&gt;
&lt;column name=&quot;rating&quot; precision=&quot;22&quot; scale=&quot;0&quot; not-null=&quot;true&quot; /&gt;
&lt;/property&gt;
&lt;property name=&quot;dateCreated&quot; type=&quot;timestamp&quot;&gt;
&lt;column name=&quot;date_created&quot; length=&quot;0&quot; /&gt;
&lt;/property&gt;
&lt;property name=&quot;lastUpdated&quot; type=&quot;timestamp&quot;&gt;
&lt;column name=&quot;last_updated&quot; length=&quot;0&quot; /&gt;
&lt;/property&gt;
&lt;/class&gt;
&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:

确定