【问题标题】:<AST>:1:33: unexpected AST node error for a HQL insert<AST>:1:33: HQL 插入的意外 AST 节点错误
【发布时间】:2015-10-12 13:46:21
【问题描述】:

这是我的实体类:

@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

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

【问题讨论】:

标签: java hibernate hql


【解决方案1】:

INSERT INTO ... VALUES 在 HQL 中不受支持,因此会出现错误。只有INSERT INTO ... SELECT。有关详细信息,请参阅文档的 DML-style operations 部分:

INSERT 语句的伪语法是:INSERT INTO EntityName properties_list select_statement。需要注意的几点:

仅支持INSERT INTO ... SELECT ... 形式;不是INSERT INTO ... VALUES ... 表单。

如果您想执行INSERT INTO ... VALUES ...,您可能应该使用SQL 和session.createNativeQuery()

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-28
      • 2014-08-24
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      相关资源
      最近更新 更多