簡體   English   中英

Hibernate條件使用GROUP BY和RETURN ENTITY LIST

[英]Hibernate criteria Using GROUP BY and RETURN ENTITY LIST

我正在嘗試在我的標准中使用GROUP BY 我需要這樣做:

SELECT b FROM Book b GROUP BY volumeCode;

我有以下代碼:

    Criteria c = s.createCriteria(Book.class);
    c.setProjection(Projections.projectionList().add(Projections.groupProperty("volumeCode")));
    List<Book> result = c.list();

但是此標准僅返回volumeCode (字符串列表)。 我需要獲得Book的列表。 所以我嘗試使用變形金剛:

    Criteria c = s.createCriteria(Book.class);
    c.setProjection(Projections.projectionList().add(Projections.groupProperty("volumeCode")));
    c.setResultTransformer(Transformers.aliasToBean(Book.class));
    List<Book> result = c.list();

此代碼返回空值列表。 是否有可能用標准來做到這一點?

首先,projecton過濾檢索的數據量,如果你想要更多的數據,你也應該將這些屬性添加到投影中。

例:

c.setProjection( Projections.projectionList()
    .add( Projections.property("id").as("id") )
    .add( Projections.property("descripction").as("description") )
    .add( Projections.groupProperty("volumeCode").as("volumeCode") ));

現在,轉換器執行它所說的“Alias to Bean”,它與java bean“Book.java”的屬性進行別名匹配。

編輯:

如果沒有變換器,如果投影具有多個屬性,結果將如下所示:

for(Object[] item:criteria.list()){
    System.out.println( (String)item[0] ); //ID
    System.out.println( (String)item[1] ); //Description
    System.out.println( (String)item[2] ); //Volume code
}

這就是為什么你得到關於變換器的強制轉換異常的原因,試圖將每個別名與你的java bean的屬性名稱相匹配。

cz_Nesh。 抱歉我的第一個答案。 我讀了Hibernate api並閱讀了一些我發現的Hibernate源代碼。 如果你使用這個代碼

session.createCriteria(EmpUserImpl.class).list();  

它將返回List EmpUserImpl。 如果你使用這個代碼

        criteria.setProjection(Projections.projectionList()
            .add(Projections.groupProperty("company").as("company"))
            .add(Projections.property("name").as("name"))
            .add(Projections.property("company").as("company")));
        List list = criteria.list();

它會返回List,不是List EmpUserImpl為什么? 我看到標准的父類CriteriaSpecification我找到了。

public interface CriteriaSpecification {

/**
 * The alias that refers to the "root" entity of the criteria query.
 */
public static final String ROOT_ALIAS = "this";

/**
 * Each row of results is a <tt>Map</tt> from alias to entity instance
 */
public static final ResultTransformer ALIAS_TO_ENTITY_MAP = AliasToEntityMapResultTransformer.INSTANCE;

/**
 * Each row of results is an instance of the root entity
 */
public static final ResultTransformer ROOT_ENTITY = RootEntityResultTransformer.INSTANCE;

/**
 * Each row of results is a distinct instance of the root entity
 */
public static final ResultTransformer DISTINCT_ROOT_ENTITY = DistinctRootEntityResultTransformer.INSTANCE;

/**
 * This result transformer is selected implicitly by calling <tt>setProjection()</tt>
 */
public static final ResultTransformer PROJECTION = PassThroughResultTransformer.INSTANCE;

/**
 * Specifies joining to an entity based on an inner join.
 *
 * @deprecated use {@link org.hibernate.sql.JoinType#INNER_JOIN}
 */
@Deprecated
public static final int INNER_JOIN = JoinType.INNER_JOIN.getJoinTypeValue();

/**
 * Specifies joining to an entity based on a full join.
 *
 * @deprecated use {@link org.hibernate.sql.JoinType#FULL_JOIN}
 */
@Deprecated
public static final int FULL_JOIN = JoinType.FULL_JOIN.getJoinTypeValue();

/**
 * Specifies joining to an entity based on a left outer join.
 *
 * @deprecated use {@link org.hibernate.sql.JoinType#LEFT_OUTER_JOIN}
 */
@Deprecated
public static final int LEFT_JOIN = JoinType.LEFT_OUTER_JOIN.getJoinTypeValue();

}

你能看到public static final ResultTransformer PROJECTION嗎? 它說這個結果變換器是通過調用setProjection()隱式選擇的,當你使用criteria.setProjection時,結果將不會列出EmpUserImpl,因為ResultTransformer從“ROOT_ENTITY”變為“PROJECTION”。它將按照Projection包裝(如選擇名稱,oid ..)。 所以,如果你想返回List EmpUserImpl,你需要設置Projections.property(“name”)。as(“name”)。,(如果你需要名字只是設置名稱)。 這是我的代碼。

        Criteria criteria = session.createCriteria(EmpUserImpl.class);
    criteria.setProjection(Projections.projectionList()
            .add(Projections.groupProperty("company").as("company"))
            .add(Projections.property("name").as("name"))
            .add(Projections.property("company").as("company")));
    criteria.setResultTransformer(Transformers.aliasToBean(EmpUserImpl.class));
    List<EmpUserImpl> list = criteria.list();
    for (EmpUserImpl empUserImpl : list) {
        System.out.println(empUserImpl.getName());
    }

它可以工作。 我希望它可以幫助你。

我想你可以使用: criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM