英文:
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("from Rating as rating where rating.organization.idorganization = :idorganization");
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 'from Rating as rating where rating.organization.idorganization = 65' 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
<?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>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论