2016-03-24 105 views
0

我期待this tutorial學習使用hibernate。 所有作品與sakila分貝。休眠第一個hql查詢...錯誤

現在,我想這方面的知識應用到我的數據庫,但是當我嘗試運行的HQL查詢(簡稱「來自用戶」),我得到一個錯誤:

org.hibernate.exception.SQLGrammarException: could not extract ResultSet 
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80) 
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) 
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) 
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89) 
at org.hibernate.loader.Loader.getResultSet(Loader.java:2065) 
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862) 
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838) 
at org.hibernate.loader.Loader.doQuery(Loader.java:909) 
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) 
at org.hibernate.loader.Loader.doList(Loader.java:2551) 
at org.hibernate.loader.Loader.doList(Loader.java:2537) 
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367) 
at org.hibernate.loader.Loader.list(Loader.java:2362) 
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:939) 
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229) 
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260) 
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103) 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from limit 100' at line 1 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) 
at com.mysql.jdbc.Util.getInstance(Util.java:387) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) 
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962) 
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80) 
... 15 more 

這是相同的如果我在教程中寫下小寫(「從電影」而不是「從電影」),則會出錯。

User.java

package model.pojos; 
import java.util.HashSet; 
import java.util.Set; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import static javax.persistence.GenerationType.IDENTITY; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.ManyToOne; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

/** 
* User generated by hbm2java 
*/ 
@Entity 
@Table(name="user" 
    ,catalog="mywheel" 
) 
public class User implements java.io.Serializable { 
private Integer oid; 
private UserType userType; 
private String username; 
private String password; 
private String email; 
private String address; 
private Set<Comments> commentses = new HashSet<Comments>(0); 
private Set<Orthopedy> orthopedies = new HashSet<Orthopedy>(0); 
private Set<Multimedia> multimedias = new HashSet<Multimedia>(0); 
private Set<Wheelchair> wheelchairs = new HashSet<Wheelchair>(0); 
private Set<Wheelchair> wheelchairs_1 = new HashSet<Wheelchair>(0); 

public User() { 
} 


public User(UserType userType, String username, String password, String email) { 
    this.userType = userType; 
    this.username = username; 
    this.password = password; 
    this.email = email; 
} 
public User(UserType userType, String username, String password, String email, String address, Set<Comments> commentses, Set<Orthopedy> orthopedies, Set<Multimedia> multimedias, Set<Wheelchair> wheelchairs, Set<Wheelchair> wheelchairs_1) { 
    this.userType = userType; 
    this.username = username; 
    this.password = password; 
    this.email = email; 
    this.address = address; 
    this.commentses = commentses; 
    this.orthopedies = orthopedies; 
    this.multimedias = multimedias; 
    this.wheelchairs = wheelchairs; 
    this.wheelchairs_1 = wheelchairs_1; 
} 

@Id @GeneratedValue(strategy=IDENTITY) 


@Column(name="oid", unique=true, nullable=false) 
public Integer getOid() { 
    return this.oid; 
} 

public void setOid(Integer oid) { 
    this.oid = oid; 
} 

@ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="user_type_oid", nullable=false) 
    public UserType getUserType() { 
     return this.userType; 
    } 


public void setUserType(UserType userType) { 
    this.userType = userType; 
} 


@Column(name="username", nullable=false, length=15) 
public String getUsername() { 
    return this.username; 
} 

public void setUsername(String username) { 
    this.username = username; 
} 


@Column(name="password", nullable=false, length=15) 
public String getPassword() { 
    return this.password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 


@Column(name="email", nullable=false, length=30) 
public String getEmail() { 
    return this.email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 


@Column(name="address", length=50) 
public String getAddress() { 
    return this.address; 
} 

public void setAddress(String address) { 
    this.address = address; 
} 

@OneToMany(fetch=FetchType.LAZY, mappedBy="user") 
    public Set<Comments> getCommentses() { 
     return this.commentses; 
    } 

public void setCommentses(Set<Comments> commentses) { 
    this.commentses = commentses; 
} 

@OneToMany(fetch=FetchType.LAZY, mappedBy="user") 

public Set<Orthopedy> getOrthopedies() { 
    return this.orthopedies; 
} 

public void setOrthopedies(Set<Orthopedy> orthopedies) { 
    this.orthopedies = orthopedies; 
} 

@OneToMany(fetch=FetchType.LAZY, mappedBy="user") 

public Set<Multimedia> getMultimedias() { 
    return this.multimedias; 
} 

public void setMultimedias(Set<Multimedia> multimedias) { 
    this.multimedias = multimedias; 
} 

@ManyToMany(fetch=FetchType.LAZY) 
    @JoinTable(name="favorite", catalog="mywheel", joinColumns = { 
     @JoinColumn(name="user_oid", nullable=false, updatable=false) }, inverseJoinColumns = { 
     @JoinColumn(name="wheelchair_oid", nullable=false, updatable=false) }) 

public Set<Wheelchair> getWheelchairs() { 
    return this.wheelchairs; 
} 

public void setWheelchairs(Set<Wheelchair> wheelchairs) { 
    this.wheelchairs = wheelchairs; 
} 

@ManyToMany(fetch=FetchType.LAZY) 
    @JoinTable(name="own", catalog="mywheel", joinColumns = { 
     @JoinColumn(name="user_oid", nullable=false, updatable=false) }, inverseJoinColumns = { 
     @JoinColumn(name="wheelchair_oid", nullable=false, updatable=false) }) 

public Set<Wheelchair> getWheelchairs_1() { 
    return this.wheelchairs_1; 
} 

public void setWheelchairs_1(Set<Wheelchair> wheelchairs_1) { 
    this.wheelchairs_1 = wheelchairs_1; 
} 




} 

hibernate.config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mywheel?zeroDateTimeBehavior=convertToNull</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property> 
    <property name="hibernate.connection.autocommit">true</property> 
    <mapping class="model.pojos.User"/> 
    <mapping class="model.pojos.Multimedia"/> 
    <mapping class="model.pojos.Brand"/> 
    <mapping class="model.pojos.Permissions"/> 
    <mapping class="model.pojos.Feature"/> 
    <mapping class="model.pojos.Comments"/> 
    <mapping class="model.pojos.FeatureType"/> 
    <mapping class="model.pojos.MultimediaType"/> 
    <mapping class="model.pojos.Wheelchair"/> 
    <mapping class="model.pojos.Material"/> 
    <mapping class="model.pojos.Orthopedy"/> 
    <mapping class="model.pojos.UserType"/> 
    <mapping class="model.pojos.FrameType"/> 
    </session-factory> 
</hibernate-configuration> 

SQL表

CREATE TABLE IF NOT EXISTS `mywheel`.`user` (
    `oid` INT(11) NOT NULL AUTO_INCREMENT, 
    `username` CHAR(15) NOT NULL, 
    `password` CHAR(15) NOT NULL, 
    `email` CHAR(30) NOT NULL, 
    `address` VARCHAR(50) NULL, 
    `user_type_oid` INT NOT NULL, 
    PRIMARY KEY (`oid`), 
    INDEX `fk_user_user_type1_idx` (`user_type_oid` ASC), 
    CONSTRAINT `fk_user_user_type1` 
    FOREIGN KEY (`user_type_oid`) 
    REFERENCES `mywheel`.`user_type` (`oid`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB 
AUTO_INCREMENT = 3 
DEFAULT CHARACTER SET = latin1; 

我使用阿魯但我也有與xml映射相同的問題。

UPDATE 隨便我改變到另一個分區我的項目的路徑... 所以現在我有C:\應用和d:\應用程序。

在NetBeans

的第一個項目(在C :)給我我以前寫的錯誤... 第二個(d :)它返回另一個錯誤:

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: model.pojos.Wheelchair.materials[model.pojos.Material] 
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1134) 
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:793) 
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728) 
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70) 
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695) 
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) 

或... XML映射沒有註釋

org.hibernate.MappingException: An association from the table wc_material refers to an unmapped class: model.pojos.Material 
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1805) 
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1739) 
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) 

也許它取決於安裝的分區XAMPP(D :)? 我不這麼認爲......因爲sakila的分貝用C正常工作:...

在這種情況下,誤差約爲未映射類

...但在hibernate.reveng.xml中我加入我的數據庫的所有表,但它並沒有創造的所有類

hibernate.reveng.xml and generated classes

+0

什麼版本的MySQL是你的版本?看起來像方言問題。你的休眠配置文件說方言爲'org.hibernate.dialect.MySQLDialect',這在MySQL ver <5.x中是有效的。如果您使用的是更高版本的MySQL,則需要將其更改爲正確的方言,如'org.hibernate.dialect.MySQL5Dialect/MySQL5InnoDBDialect /'等。 –

+0

我已經嘗試過......這不是問題...也因爲sakila db WORKS – Marco

+0

[半解析]好的,解決第一個問題,我不得不清理並構建我的項目... – Marco

回答

0

的錯誤信息是很清楚的:from limit 100

你錯過了表,從您的查詢選擇。

...from `YourTable` limit 100

但可以發佈您的查詢一定會幫助了。

+0

查詢來自用戶...我寫了 – Marco

0

根案例:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from limit 100' at line 1 

指定你是從取表名。似乎你錯過了它。

也嘗試在休眠配置中更改MySQL方言。

使用

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> 

,而不是

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

注意:每個人都應該小心hibernate.cfg.xml文件表的名稱,因爲它應該是在查詢相同(區分大小寫) 。

+0

查詢很清楚......「來自用戶」。 ..表是用戶!... :( – Marco

+0

相同的錯誤...它就像它不能在db中找到表...事實上,我有同樣的問題,如果在netbeans教程我寫在hql查詢中使用小寫字母的表格=( – Marco

+0

已添加更新... – Marco

-1

它也發生在我身上,你必須清理並通過右鍵點擊它來構建項目