<ast>:1:33:HQL插入的意外AST节点错误

时间:2015-10-12 13:46:21

标签: java hibernate hql

这是我的实体类:

@Entity
@Table(name = "courier_upload_queue")
public class CourierUploadQueue  {

    private int id;
    private String crp_code;
    private Status status;
    private Date time_created;
    private Date last_update;
    private String courier_name;
    private Integer retry_count;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

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

    @Enumerated(EnumType.STRING)
    @Column(name= "status", nullable= false)
    public Status getStatus() {
        return status;
    }

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

    @Column(name= "crp_code", unique = true, nullable = false)
    public String getCrp_code() {
        return crp_code;
    }

    public void setCrp_code(String crp_code) {
        this.crp_code = crp_code;
    }

    @Column(name="time_created", nullable = false)
    public Date getTime_created() {
        return time_created;
    }

    public void setTime_created(Date time_created) {
        this.time_created = time_created;
    }

    @Column(name="last_update", nullable = false)
    public Date getLast_update() {
        return last_update;
    }

    public void setLast_update(Date last_update) {
        this.last_update = last_update;
    }


    @Column(name="courier_name", nullable = false)
    public String getCourier_name() {
        return courier_name;
    }

    public void setCourier_name(String courier_name) {
        this.courier_name = courier_name;
    }

    @Column(name="retry_count", nullable = false)
    public Integer getRetry_count() {
        return retry_count;
    }

    public void setRetry_count(Integer retry_count) {
        this.retry_count = retry_count;
    }

}

这是我的DaoImpl

@Override
public void insertToCourierUploadQueue(String crp_code, String courier_name)
{
    Query query = sessionFactory.getCurrentSession().createQuery("insert into CourierUploadQueue (:crp_code, 'PENDING', CURRENT_TIMESTAMP() , CURRENT_TIMESTAMP() , :courier_name, 0 )");
    query.setParameter("crp_code", crp_code);
    query.setParameter("courier_name", courier_name.toLowerCase());
    query.executeUpdate();  
}

我在我的数据库中创建了相应的表。

我收到错误消息:

  

无法解析属性::: com.pooja.entity.CourierUploadQueue

你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

HQL不支持

INSERT INTO ... VALUES,因此出错。只有1个}}。有关详细信息,请参阅文档的DML-style operations部分:

  

INSERT语句的伪语法是:INSERT INTO ... SELECT。有些要点需要注意:

     

仅支持INSERT INTO EntityName properties_list select_statement表单;不是INSERT INTO ... SELECT ...形式。

如果您想执行INSERT INTO ... VALUES ...,您应该使用SQL和INSERT INTO ... VALUES ...代替。

答案 1 :(得分:0)

Bohuslav是对的。在HQL中不支持INSERT INTO ... VALUES。

相反,我这样做了:

public void insertToCourierUploadQueue(String crp_code, String courier_name){Session session1 = sessionFactory.openSession();
        CourierUploadQueue courierUploadQueue = new CourierUploadQueue();
        courierUploadQueue.setCrp_code(crp_code);
        courierUploadQueue.setCourier_name(courier_name);
        courierUploadQueue.setLast_update(new Date());
        courierUploadQueue.setTime_created(new Date());
        courierUploadQueue.setRetry_count(0);
        courierUploadQueue.setStatus(CourierUploadQueueStatus.Status.PENDING);
        session1.save(courierUploadQueue);
        session1.close();}