1

I'm trying to insert a record into the booking table using the following method,

DAO method

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;
    }

Error

 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]

Booking Domain class

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;
    }



}

If I didn't set the value to created field, it inserts a null value - but, it defaults to CURRENT_TIMESTAMP in mysql table column. When i set its value, it throws another syntax error as below

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

1 Answer 1

3

desc is keyword. You need to quote it with backtick or rename column:

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.