cz_Nesh. Entschuldigung wegen meiner ersten Antwort. Ich lese Hibernate API und lese einige Hibernate Quellcode ich finde das. wenn Sie diesen Code verwenden
session.createCriteria(EmpUserImpl.class).list();
es wird Liste EmpUserImpl zurück. wenn Sie diesen Code verwenden
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();
wird Liste zurückkehren, Liste ist nicht EmpUserImpl warum? Ich sehe die Elternklasse des Kriteriums CriteriaSpecification ich finde das.
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();
}
können Sie sehen, die public static final ResultTransformer PROJECTION? es besagt, dass dieser Ergebnistransformator implizit durch Aufruf von setProjection() ausgewählt wird ist der Mittelwert, wenn Sie criteria.setProjection verwenden, wird das Ergebnis nicht EmpUserImpl auflisten, da ResultTransformer in "PROJECTION" von "ROOT_ENTITY" geändert wird. wie wähle Namen, oid ..). Wenn Sie also List EmpUserImpl zurückgeben möchten, müssen Sie Projections.property ("name"). As ("name")., (Wenn Sie einen Namen brauchen, setzen Sie den Namen). das ist mein Code.
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());
}
kann es funktionieren. Ich hoffe, es kann dir helfen.
Hallo Ziul, thx für Ihre Antwort. Mit Ihrem Rat bekomme ich Liste der Objekte, aber Java löst Exception aus, wenn ich versuche, ein beliebiges Objekt aus der Liste in das Buch zu schreiben. –
Ehm, ich lösche Zeile mit Transformer O: -) ... Nach dem Hinzufügen dieser Zeile bekomme ich noch eine Liste von Null-Objekten. –
Vielen Dank für die Bearbeitung Ihres Posts. Ich will nicht nur eine Eigenschaft, sondern das ganze Objekt bekommen. Ich brauche die gleiche Liste von Objekten wie nach dem Aufruf 'SELECT b FROM Buch b GROUP BY volumeCode', so dass das Ergebnis sein sollte Liste –