java - 插入时HIbernate语法sql错误

标签 java mysql sql hibernate

我正在尝试使用以下方法将记录插入到预订表中,

DAO 方法

public boolean addBooking(String title,String desc,int slotid,int subscriberid,Session session){
        Booking booking = new Booking();
        booking.setTitle(title);
        booking.setDesc(desc);
        booking.setSlotid(slotid);
        booking.setSubscriberid(subscriberid);
        //booking.setCreated(new Date());
        Integer bookingid = (Integer) session.save(booking);
        if(bookingid > 0)
        return true;
        return false;
    }

错误

 insert 
    into
        BOOKING
        (created, desc, slotid, subscriberid, title) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        BOOKING
        (created, desc, slotid, subscriberid, title) 
    values
        (?, ?, ?, ?, ?)
2015-11-01 00:43:00 DEBUG SqlExceptionHelper:139 - 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 'desc, slotid, subscriberid, title) values (null, 'hello12', 24, 39, 'hello1')' at line 1 [n/a]

预订域类

import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;


@Entity
@Table(name = "BOOKING")
public class Booking {

    public Booking(){

    }

    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

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

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


    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "slotid",insertable = false, updatable = false)
    private Slot slot;

    private Integer slotid;

    private Integer subscriberid;


    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "subscriberid",insertable = false, updatable = false)
    private User subscriber;

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

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Slot getSlot() {
        return slot;
    }

    public void setSlot(Slot slot) {
        this.slot = slot;
    }

    public User getSubscriber() {
        return subscriber;
    }

    public void setSubscriber(User subscriber) {
        this.subscriber = subscriber;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getSlotid() {
        return slotid;
    }

    public void setSlotid(Integer slotid) {
        this.slotid = slotid;
    }

    public Integer getSubscriberid() {
        return subscriberid;
    }

    public void setSubscriberid(Integer subscriberid) {
        this.subscriberid = subscriberid;
    }



}

如果我没有将值设置为创建的字段,它会插入一个空值 - 但是,它默认为 mysql 表列中的 CURRENT_TIMESTAMP。当我设置它的值时,它会抛出另一个语法错误,如下所示

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 'desc, slotid, subscriberid, title) values ('2015-11-01 00:47:25', 'hello12', 24,' at line 1

最佳答案

desc 是关键字。您需要用反引号引用它或重命名列:

insert into BOOKING
        (created, `desc`, slotid, subscriberid, title) 
values

5.4. SQL quoted identifiers :

You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.

@Column(name = "`desc`")
private String desc;

关于java - 插入时HIbernate语法sql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33455611/

相关文章:

SQL:如何在不使用函数的情况下从表中获取上一个日期

Java - 在浏览时将元素添加到 ArrayList

javascript - 以PHP形式以表格方式显示数据

mysql - 如何根据单次更新查询的另一列中的值更新一列中的值?

mysql - 从 mysql 到 Yii 语法

mysql - sql : Scan error on column index 6, 名称 "scheduled_date": null: cannot scan type []uint8 into null. 时间

java - 在运行时使用参数编写自定义 Kafka SSLEngineFactory

java - 如何创建用于拆分请求参数并收集返回结果的自定义注释?

java - Uber API - 在沙盒中请求乘车 - 接收错误 : Invalid request - validation_failed

mysql - MYSQL 有没有类似 mt_rand 的函数