将数据记录保存到数据库时SQL语法错误

时间:2014-08-09 11:30:40

标签: java mysql sql hibernate struts

将数据记录保存到数据库时,SQL语法出错:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'option, createdBy, createdDate, updatedBy, updatedDate, description) values ('QU' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
    ... 86 more

获取会话方法getSession

Struts代码:

public T saveOrUpdate(T entity) {
        try {
            getSessionTx().save(entity);
            commit();
        } catch (Exception e) {
            rollback();
            e.printStackTrace();
        } finally {
            getSession().close();
        }
        return entity;
    }


    for(int i = 0;i<appendText.size();i++){
        PlayerPredictionOptionLevel2 playerPredictionOptionLevel2 = new PlayerPredictionOptionLevel2();
        playerPredictionOptionLevel2.setPlayerPredictionLevel2Id(str);
        playerPredictionOptionLevel2.setOption(appendText.get(i));
        getDaoFactory().getPlayerPredictionOptionLevel2Dao().saveOrUpdate(playerPredi‌​ctionOptionLevel2);
    }

Hibernate:

insert into expert_predictor.player_prediction_option_level2 
    (player_prediction_level2_id, option, createdBy, createdDate, updatedBy, updatedDate, description) 
  values 
    (?, ?, ?, ?, ?, ?, ?)

实体映射:

我已经使用我的数据库检查了我的实体,但仍有问题 有一件事这个表没有任何关系使用唯一键我管理了所有的代码。 请帮我解决这个问题

package com.expertPredictor.model;

import java.util.Date;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author CHiRAG
 */
@Entity
@Table(name = "player_prediction_option_level2", catalog = "expert_predictor", schema = "")
@XmlRootElement

public class PlayerPredictionOptionLevel2 implements Model {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Column(name = "player_prediction_option_level2_ID")
    private Integer playerpredictionoptionlevel2ID;


    @Column(name = "player_prediction_level2_id")
    private String playerPredictionLevel2Id;

    @Column(name = "option")
    private String option;

    @Column(name = "createdBy")
    private String createdBy;

    @Column(name = "createdDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;

    @Column(name = "updatedBy")
    private String updatedBy;

    @Column(name = "updatedDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedDate;

    @Column(name = "description")
    private String description;

    public PlayerPredictionOptionLevel2() {
    }

    public PlayerPredictionOptionLevel2(Integer playerpredictionoptionlevel2ID) {
        this.playerpredictionoptionlevel2ID = playerpredictionoptionlevel2ID;
    }

    public PlayerPredictionOptionLevel2(Integer playerpredictionoptionlevel2ID, String playerPredictionLevel2Id, String option, String createdBy, Date createdDate, String updatedBy, Date updatedDate) {
        this.playerpredictionoptionlevel2ID = playerpredictionoptionlevel2ID;
        this.playerPredictionLevel2Id = playerPredictionLevel2Id;
        this.option = option;
        this.createdBy = createdBy;
        this.createdDate = createdDate;
        this.updatedBy = updatedBy;
        this.updatedDate = updatedDate;
    }

    public Integer getPlayerpredictionoptionlevel2ID() {
        return playerpredictionoptionlevel2ID;
    }

    public void setPlayerpredictionoptionlevel2ID(Integer playerpredictionoptionlevel2ID) {
        this.playerpredictionoptionlevel2ID = playerpredictionoptionlevel2ID;
    }

    public String getPlayerPredictionLevel2Id() {
        return playerPredictionLevel2Id;
    }

    public void setPlayerPredictionLevel2Id(String playerPredictionLevel2Id) {
        this.playerPredictionLevel2Id = playerPredictionLevel2Id;
    }

    public String getOption() {
        return option;
    }

    public void setOption(String option) {
        this.option = option;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public Date getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (playerpredictionoptionlevel2ID != null ? playerpredictionoptionlevel2ID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PlayerPredictionOptionLevel2)) {
            return false;
        }
        PlayerPredictionOptionLevel2 other = (PlayerPredictionOptionLevel2) object;
        if ((this.playerpredictionoptionlevel2ID == null && other.playerpredictionoptionlevel2ID != null) || (this.playerpredictionoptionlevel2ID != null && !this.playerpredictionoptionlevel2ID.equals(other.playerpredictionoptionlevel2ID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.expertPredictor.model.PlayerPredictionOptionLevel2[ playerpredictionoptionlevel2ID=" + playerpredictionoptionlevel2ID + " ]";
    }

    @Override
    public EntityStatus getStatus() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setStatus(EntityStatus status) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:7)

hibernate生成的SQL中存在SQL语法错误。 hibernate根据配置设置使用的方言生成SQL,并且操作正确,因此如果您使用了HQL或条件查询,则不会出现错误。如果您使用的是MySQL JDBC驱动程序,那么您将连接到MySQL数据库。 HQL使用对象映射来获取查询使用的表和列的名称。如果您通过hibernate会话对象使用save,则会生成insert SQL语句。错误

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在&#39;选项, createdBy,createdDate,updatedBy,updatedDate,description)值附近使用正确的语法(&#39; QU&#39;在第1行

显示哪个列名错误。您也可以从MySQL参考手册中查看Reserved Words

如果它们被反引号,通常可以在SQL中使用这些名称。例如,将列名指定为`option`。但它具有MySQL特定性,最好将其重命名为非保留的内容。