Hibernate第一次hql查询...错误

时间:2016-03-24 10:16:30

标签: java hibernate

我期待this tutorial学习使用hibernate。 所有这些都适用于sakila db。

现在我试图将这些知识应用到我的数据库,但是当我尝试运行HQL查询(简单地说#34;来自用户")时,我收到错误:

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映射相同的问题。

更新 随便我将项目的路径改为另一个分区...... 所以现在我有C:... \ app和D:... \ app。

在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 db在C中运行良好:...

在这种情况下,错误是关于未映射的类...但是在 hibernate.reveng.xml 中我添加了我的数据库的所有表,但它没有创建所有类

hibernate.reveng.xml and generated classes

3 个答案:

答案 0 :(得分:0)

错误消息非常清楚:from limit 100

您在查询中遗漏了要从中进行选择的表格。

...from `YourTable` limit 100

但发布您的查询肯定也有帮助。

答案 1 :(得分:0)

Root case:

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

指定要从中获取的表名。好像你错过了它。

还尝试在hibernate配置中更改MySQL方言。

使用

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

而不是

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

注意:应该注意hibernate.cfg.xml文件中的表名,因为它在查询中应该是相同的(区分大小写)。

答案 2 :(得分:-1)

它也发生在我身上,你必须通过右击它来清理和构建项目