Table Players
:
ID | name | email | age | ...
1 | \'bob\' | null | 23 | ...
This table is where instances of class Pl
Use a query with AliasToEntityMapResultTransformer
; is verbose but should works with Hibernate property definition and not with JavaBean definition (they can differ).
Map aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setProjection(Projections.projectionList()
.add(Projections.id().as("id"))
// Add others properties
)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();
A worse approch can be write a custom ResultTransformer that introspect ClassMetadata and try to extract values...
class IntrospectClassMetadata extends BasicTransformerAdapter {
PassThroughResultTransformer rt = PassThroughResultTransformer.INSTANCE;
public Object transformTuple(Object[] tuple, String[] aliases) {
final Object o = rt.transformTuple(tuple, aliases);
ClassMetadata cm = sf.getClassMetadata(o.getClass());
List pns = new ArrayList(Arrays.asList(cm.getPropertyNames()));
Map m = new HashMap();
for(String pn : pns) {
m.put(pn, cm.getPropertyValue(o, pn));
}
m.put(cm.getIdentifierPropertyName(), cm.getIdentifier(o));
return m;
}
}
and use
Map aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setResultTransformer(new IntrospectClassMetadata())
.uniqueResult();
Last chance:
Map map = (Map)s.createSQLQuery("select * from user where id = :id")
.setParameter("id",p.id)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();
but this doesn't map list,bags and other mapped object, but only raw column names and values...