问题描述
表玩家
:
ID |名称|电子邮件|年龄| ...
1 | 'bob'| null | 23 | ...
此表是类 示例用法: 使用 更糟的是,可以编写一个自定义ResultTransformer来反省ClassMetadata并尝试提取值... 并使用 最后机会: 但这并不映射列表,行李和其他映射对象,但只有原始列名称和值... Table This table is where instances of class Having a Hibernate Example usage: Use a query with A worse approch can be write a custom ResultTransformer that introspect ClassMetadata and try to extract values... and use Last chance: but this doesn't map list,bags and other mapped object, but only raw column names and values... 这篇关于在DB中作为Hibernate中的Map检索一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! Player $ c
$ b 拥有一个Hibernate Session
,$ c $是持久的(每个实例一行, 我如何获得行(例如用id - PK - 等于1)作为Java Map (key =列名,值=单元格值)?
Map< String,String> row = getPlayerByIdAsMap(1);
AliasToEntityMapResultTransformer
;是冗长的,但应该与Hibernate属性定义一起使用,而不是与JavaBean定义一起使用(它们可以有所不同)。
Map< String,Object> aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setProjection(Projections.projectionList()
.add(Projections.id ().as(id))
//添加其他属性
)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();
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());
列表< String> pns = new ArrayList< String>(Arrays.asList(cm.getPropertyNames()));
地图< String,Object> m = new HashMap< String,Object>();
for(String pn:pns){
m.put(pn,cm.getPropertyValue(o,pn));
}
m.put(cm.getIdentifierPropertyName(),cm.getIdentifier(o));
返回m;
}
}
映射< String,Object> aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setResultTransformer(new IntrospectClassMetadata())
.uniqueResult();
地图<字符串,对象> map =(Map< String,Object>)s.createSQLQuery(select * from user where id =:id)
.setParameter(id,p.id)
.setResultTransformer(AliasToEntityMapResultTransformer。 INSTANCE)
.uniqueResult();
Players
:ID | name | email | age | ...
1 | 'bob' | null | 23 | ...
Player
are persisted (one row per instance, no composition etc.).Session
, how do I get the row (say with id - the PK - equal to 1) as a Java Map (key = column name, value = cell value) ?Map<String,String> row = getPlayerByIdAsMap(1);
AliasToEntityMapResultTransformer
; is verbose but should works with Hibernate property definition and not with JavaBean definition (they can differ).Map<String,Object> aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setProjection(Projections.projectionList()
.add(Projections.id().as("id"))
// Add others properties
)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();
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<String> pns = new ArrayList<String>(Arrays.asList(cm.getPropertyNames()));
Map<String, Object> m = new HashMap<String, Object>();
for(String pn : pns) {
m.put(pn, cm.getPropertyValue(o, pn));
}
m.put(cm.getIdentifierPropertyName(), cm.getIdentifier(o));
return m;
}
}
Map<String,Object> aliasToValueMap =
session.createCriteria(User.class)
.add(Restrictions.idEq(userID))
.setResultTransformer(new IntrospectClassMetadata())
.uniqueResult();
Map<String,Object> map = (Map<String,Object>)s.createSQLQuery("select * from user where id = :id")
.setParameter("id",p.id)
.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)
.uniqueResult();