Hibernate,Criteria,Projection,Transformer.aliasToBean和List <t>问题

时间:2015-05-11 13:07:23

标签: java hibernate entity criteria projection

我想选择一个具有一些确定字段的实体。其他字段为NULL。 我的问题......

EntA已经重新获得EntB和EntC列表。

class EntA {
    @Id
    @GenericGenerator(name = "unique_id", strategy = "uuid")
    @GeneratedValue(generator = "unique_id")
    @Column(name = "id", nullable = false, length = 32, unique = true)
    private String id;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "entbs", nullable = false, unique = false)
    private EntB entB;
    @OneToMany(mappedBy = "entcs", fetch = FetchType.LAZY)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIgnore
    private List<EntC> listEntC;
    ... other fields , getters & setters ...
}

几天前,我用下面的代码补充了bacis选择程序。代码提供选择具有一些确定字段的对象,其他字段将为空。有些人喜欢SQL`s&#34;选择fieldA,fieldJ from table ...&#34;

public List<EntA> select(..., List<String> fieldList, ...) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(EntA.class);

    ...

    ProjectionList pl = Projections.projectionList();
    for (String prop : fieldList) {
        pl.add(Projections.property(prop), prop);
    }
    crit.setProjection(pl);
    crit.setResultTransformer(Transformers.aliasToBean(EntA.class));

    ...

    return crit.list();
}

一切都很好,例如:我选择一些字段(不是所有实体)甚至重复一些字段。结果=类EntA的对象,其具有字段&#34; id&#34;,&#34; EntB&#34;和&#34; other_field_name&#34;使用值,其他字段为NULL。

List<String> fieldList = new ArrayList<String>();
fieldList.add("id");
fieldList.add("entB");
fieldList.add("entB");
fieldList.add("entB");
fieldList.add("other_field_name");
select(..., fieldList, ...); 
Hibernate: 
    /* criteria query */ select
        this_.id as y0_,
        this_.entB as y1_,
        this_.entB as y2_,
        this_.entB as y3_,
        this_.other_field_name as y4_
    from
        EntA this_
Hibernate: 
    select
        entb0_.id as id1_12_0_,
        entb0_.idx as idx2_12_0_,
        entb0_.name as name3_12_0_ 
    from
        EntB entb0_ 
    where
        entb0_.id=?

但是...

fieldList.add("id");
fieldList.add("listEntC");
select(..., fieldList, ...);
Hibernate: 
    /* criteria query */ select
        this_.id as y0_,
        this_.id as y1_ 
    from
        EntA this_
java.lang.ArrayIndexOutOfBoundsException: 1
    at org.hibernate.loader.criteria.CriteriaLoader.getResultRow(CriteriaLoader.java:168)
    at org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:148)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:753)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    *************************************.select(BaseDao.java:141)

几天我调试我的代码&amp; hibernate-core来源并且无法理解:
如果我使用没有投影和变换器的简单选择,我得到EntA的正常对象List&lt; EntC&gt; lstEntC具有值
如果我使用没有变压器的投影 - 我得不到EntA
如果我使用变换器投影,a只能选择非列表字段

您对如何将Criteria的选择与投影结果一起用于课程有任何想法吗? 感谢。

0 个答案:

没有答案