带连接的HQL Criterion

时间:2013-05-09 12:20:10

标签: hibernate

我有一个用例来显示使用完整实体中值的子集的实体列表。我采用的方法是创建一个EntityList类,只显示列表中显示的那些字段。此类映射到与完整实体相同的表,但只有一部分字段。

使用HQL,我想根据完整实体中的字段过滤返回的EntityList。在下面的示例中,我希望在Entity的描述字段上过滤EntityList(它在表中,但不在EntityList类中)。

public interface IThreePhaseMotorList {
abstract public Long getId();
abstract public String getMfg();
abstract public Double getPowerUnits();
abstract public Integer getPoles();
}

public interface IThreePhaseMotor extends IMotor {
public abstract Long getId();
public abstract void setId(Long id);
public abstract Integer getVersion();
public abstract void setVersion(Integer version);
public abstract String getIdsrc();
public abstract void setIdsrc(String idsrc);
public abstract String getDescription();
public abstract void setDescription(String description);
public abstract String getManufacturer();
public abstract void setManufacturer(String manufacturer);
public abstract Integer getPoles();
public abstract setPoles(Integer poles);
}

如果我直接针对表编写SQL,它将如下所示:

Select IThreePhaseMotorList.* 
  from IThreePhaseMotorList JOIN IThreePhaseMotor ON  
      IThreePhaseMotorList.id = IThreePhaseMotor.id
  where IThreePhaseMotor.Description like 'test%';

无论如何在HQL中执行此操作?

1 个答案:

答案 0 :(得分:1)

  

我采用的方法是创建一个EntityList类,只显示列表中显示的那些字段。此类映射到与完整实体相同的表,但只有一部分字段。

这是问题的根源。不要那样做。只需使用完整的实体进行查询即可。加载一些不需要的列通常不会对性能产生任何重大影响。如果是,则只执行一个只选择所需列的查询。