You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate 3.6 Criteria API默认表别名无法修改问题求助

问题描述

我使用Hibernate 3.6.10.Final版本的Criteria API,想要将执行SQL的表别名从默认的this修改为temp,但调用getSession().createCriteria(Vehicle.class, "temp")设置别名后并未生效。

实际生成的SQL:

/* criteria query */ select this_.vehicle_id as y0_, this_.vin as y1_,this_.initial_registration as y2_ from vehicle this_ where this_.vin=?

期望生成的SQL:

/* criteria query */ select temp.vehicle_id as y0_, temp.vin as y1_,temp.initial_registration as y2_ from vehicle temp where temp.vin=?

简化代码示例:

protected List<T> findByProjectionCriteria() {

    Criteria cr = getSession().createCriteria(Vehicle.class, "temp");
    cr.setResultTransformer(Transformers.aliasToBean(Vehicle.class));

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("vehicleId"), "vehicleId");
    projectionList.add(Projections.property("vin"), "vin");
    projectionList.add(Projections.property("initialRegistration"), "initialRegistration");
    cr.setProjection(projectionList);

    cr.add(Restrictions.eq("vin", "WVW29343249702776"));
            
    begin();
    
    List<T> list = null;
    try {
        list = cr.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (list == null)
        list = new ArrayList<T>();

    commit();
    return list;
}

实体类代码:

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.*;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name = "vehicle")
public class Vehicle {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "vehicle_id", unique = true, nullable = false)
    private int vehicleId;
    
    @Column(nullable = false, length = 17)
    private String vin;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "initial_registration")
    private Date initialRegistration;
    
}
解决方案

在Hibernate 3.6.10.Final版本中,仅通过createCriteria设置别名还不够,需要在Projection属性引用和Restrictions条件中明确带上别名前缀,这样生成SQL时才会使用指定的别名。修改后的代码如下:

protected List<T> findByProjectionCriteria() {

    Criteria cr = getSession().createCriteria(Vehicle.class, "temp");
    cr.setResultTransformer(Transformers.aliasToBean(Vehicle.class));

    ProjectionList projectionList = Projections.projectionList();
    // 投影属性前加上别名前缀
    projectionList.add(Projections.property("temp.vehicleId"), "vehicleId");
    projectionList.add(Projections.property("temp.vin"), "vin");
    projectionList.add(Projections.property("temp.initialRegistration"), "initialRegistration");
    cr.setProjection(projectionList);

    // 查询条件前加上别名前缀
    cr.add(Restrictions.eq("temp.vin", "WVW29343249702776"));
            
    begin();
    
    List<T> list = null;
    try {
        list = cr.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (list == null)
        list = new ArrayList<T>();

    commit();
    return list;
}

修改后,生成的SQL就会使用temp作为表别名,符合期望。


内容的提问来源于stack exchange,提问作者Martin_0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 02:05:58