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

问题描述 投票:0回答:1

将数据记录保存到数据库时,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);
    }

休眠:

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

    }

}
java mysql sql hibernate struts
1个回答
7
投票

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

save
,则会生成
insert
SQL 语句。错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'option,createdBy,createdDate,updatedBy,updatedDate,description)值(第 1 行的“QU”)附近使用的正确语法

显示哪个列名称是错误的。您还可以从 MySQL 参考手册中查看保留字

通常,如果这些名称被反引号引用,则可以在 SQL 中使用。例如,将列名称指定为

`option`
。但它是 MySQL 特定的,最好将其重命名为未保留的名称。

© www.soinside.com 2019 - 2024. All rights reserved.