嵌入式id成员值的Hibernate投影

时间:2019-10-03 14:26:17

标签: java hibernate jpa orm

这是我的代码

//Sample Entity Code
@Entity
public class Car {

    @EmbeddedId
    private CarPk carPk;

    private String color;

    // getters and setters...

}
@Embeddable
public class CarPk implements Serializable
{
    private static final long   serialVersionUID    = 453L;
    @Column(name = "CHESSI_NO")
    private String              chessiNo;

    @Column(name = "ENGINE_NO")
    private String              engineNo;

      //getters and setters
}

现在我正尝试如下

// Here I do not Want other details of car Suppose it has some 100 columns 
//and I want only two chessi no and color then I was trying below
// inside some method 


ProjectionList      projectionList = Projections.projectionList().add(Projections.property("chessiNo"), "chessiNo");

projectionList.add(Projections.property("color"), "color");

Criteria criteria = getSession().createCriteria(Car.class);
criteria.setResultTransformer(Transformers.aliasToBean(Car.class);

criteria.setProjection(projectionList);

criteria.add(Restrictions.eq("color", "blue"));

return criteria.gist();  // this is output of my some method 

但这会引发异常

org.hibernate.QueryException: could not resolve property: chessiNo of: com.test.Car

有什么方法可以选择投影中的嵌入ID来减少数据传输的占用空间,并减少要从表中提取的数据(而不是100列),我只需要两列,而如果需要的话,加载整个bean不是最佳解决方案在类似情况下需要循环操作,请帮忙。

0 个答案:

没有答案