我尝试使用以下方法将记录插入到预订表中,
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字段,则会插入一个空值 - 但是,它默认为mysql表列中的CURRENT_TIMESTAMP。当我设置它的值时,它会抛出另一个语法错误,如下所示
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以获得正确的语法 使用near' desc,slotid,subscriberid,title)值(' 2015-11-01 00:47:25',' hello12',24,'在第1行
答案 0 :(得分:3)
desc
是关键字。您需要使用反引号或重命名列来引用它:
insert into BOOKING
(created, `desc`, slotid, subscriberid, title)
values
<强> 5.4. SQL quoted identifiers
强>:
您可以强制Hibernate在生成的SQL中引用标识符 在映射中的反引号中包含表或列名称 文献。 Hibernate将为SQL使用正确的引用样式 方言。这通常是双引号,但SQL Server使用 括号和MySQL使用反引号。
@Column(name = "`desc`")
private String desc;