Hibernate:意外令牌:和比较日期时

时间:2015-10-31 14:47:41

标签: java hibernate hql

我通过比较使用java.util.Date映射的两个日期时间列'start'和'end'来获取两个日期之间的所有插槽

错误

unexpected token: end near line 1, column 74 [From 
com.booking.tokbox.domain.Slot where 
ownerid = ? and start >= ? and end <= ?]

DAO方法

public List<Slot> fetchSlots(Session session,int ownerid,String start,String end) throws ParseException{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date startDate = dateFormat.parse(start);
    Date endDate = dateFormat.parse(end);
    List<Slot> slots = null;
    Query query = session.createQuery("From Slot where ownerid = ? and start >= ? and end <= ?").setParameter(0,ownerid)
            .setParameter(1, startDate).setParameter(2,endDate);
    slots = query.list();
    if(slots.size() > 0)
        return slots;
    return null;
}

广告位 - 域类

import java.util.Date;
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="SLOT")
public class Slot {

    public Slot(){

    }

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

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

    @Column(name="start")
    private Date start;

    @Column(name="end")
    private Date end;

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

    private Integer ownerid;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created", nullable = false, updatable=false)
    @Version
    private Date created;

    public Integer getId() {
        return id;
    }

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

    public User getUser() {
        return this.user;
    }

    public void setUser(User owner) {
        this.user = owner;
    }


    public void setStart(Date start) {
        this.start = start;
    }

    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getOwnerid() {
        return ownerid;
    }

    public void setOwnerid(Integer ownerid) {
        this.ownerid = ownerid;
    }

    public Date getCreated() {
        return created;
    }

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

    public Date getStart() {
        return start;
    }



}

2 个答案:

答案 0 :(得分:4)

我的猜测结束是SQL中受保护的关键字。 重命名您的字段或使用别名(Slot s =&gt; s.end)

例如:https://dev.mysql.com/doc/refman/5.0/en/begin-end.html

答案 1 :(得分:0)

end是SQL中的关键字,您无法使用它。请重命名