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

标签 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

获取 session 方法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

    }

}

最佳答案

hibernate 生成的 SQL 中存在 SQL 语法错误。 hibernate 根据配置设置使用的方言生成 SQL,它做的是正确的,所以如果你使用了 HQL 或条件查询,应该不会有错误。如果您使用的是 MySQL JDBC 驱动程序,那么您将连接到 MySQL 数据库。 HQL 使用对象映射来获取查询使用的表和列的名称。如果您通过 hibernate session 对象使用save,则会生成insert SQL 语句。错误

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

显示哪个列名是错误的。您也可以查看 Reserved Words来自 MySQL 引用手册。

如果这些名称被反引号括起来,通常可以在 SQL 中使用。例如,将列名称指定为 `option`。但它是 MySQL 特定的,最好将其重命名为未保留的名称。

关于java - 将数据记录保存到数据库时出现 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25218086/

相关文章:

java - Log4j 记录器垃圾收集?

python - 使用没有模型的 django

Mysql 将默认值设置为 json 类型的列

SQL-Server 将 "USE"与 INNER JOINS 和 "UNION"一起使用

java - 如何实现 Spring Boot Controller 以 json 形式返回查询结果

java - 使用自定义比较器对 Thymeleaf 模板中的列表进行排序

php - 通过匹配子数组值合并 2 个关联数组?

php - SQL 问题 - 在两个日期之间进行选择

sql - 在 Hive 0.13 中读取 CSV 文件,数据中也不带引号和逗号

java - 背靠背多个 HttpExchange,使用 com.sun.net.httpserver.HttpServer 从第一次交换开始清理