我正试图使用以下方法将一条记录插入预订表中,
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;
}
}
如果我没有将值设置为created字段,它会插入一个null值,但在mysql表列中,它默认为CURRENT_TIMESTAMP。当我设置它的值时,它会抛出另一个语法错误,如下所示
您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以获得正确的语法使用接近"desc,slotid,subscriberid,title"的值('2015-11-0100:47:25','hello12',24'在 1号线
desc
是关键字。你需要引用它的反勾号或重命名列:
insert into BOOKING
(created, `desc`, slotid, subscriberid, title)
values
5.4. SQL quoted identifiers
:
您可以通过以下方式强制Hibernate在生成的SQL中引用标识符在映射中用反引号括起表或列名文件Hibernate将为SQL使用正确的引号样式地方话这通常是双引号,但SQL Server使用括号和MySQL使用backticks。
@Column(name = "`desc`")
private String desc;