javamysqlsqlhibernatestruts

An error in SQL syntax when saving data record into the database


I have an error in SQL syntax when saving data record into the database:

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

In Get Session Method getSession

Struts code:

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 
    (?, ?, ?, ?, ?, ?, ?)

Entity mapping:

I have checked my entity with my DB but still there is an issue and one thing this table have not any relationship using unique key I have managed all the code. Please help me to solve this problem

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

    }

}

Solution

  • You have SQL syntax error in the SQL generated by hibernate. The hibernate generates SQL according a dialect used by the configuration settings and it does it right, so there should not be errors if you have used HQL or criteria queries. If you are using MySQL JDBC driver then you are connecting to the MySQL database. HQL uses object mapping to get names for the tables and columns used by the query. If you are using save via hibernate session object then insert SQL statement is generated. The error

    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

    shows you which column name is wrong. You can also check Reserved Words from the MySQL Reference Manual.

    Usually such names can be used in the SQL if they are backquoted. For example specifying a column name as `option`. But it's MySQL specific, and better rename it to something not reserved.