在DB中作为Hibernate中的Map检索一行

在DB中作为Hibernate中的Map检索一行

本文介绍了在DB中作为Hibernate中的Map检索一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

玩家

  ID |名称|电子邮件|年龄| ... 
1 | 'bob'| null | 23 | ...

此表是类 Player $ 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();

更糟的是,可以编写一个自定义ResultTransformer来反省ClassMetadata并尝试提取值...

  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();

但这并不映射列表,行李和其他映射对象,但只有原始列名称和值...

Table Players:

ID | name  | email | age | ...
1  | 'bob' | null  | 23  | ...

This table is where instances of class Player are persisted (one row per instance, no composition etc.).

Having a Hibernate 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) ?

Example usage:

Map<String,String> row = getPlayerByIdAsMap(1);
解决方案

Use a query with 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();

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<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;
  }
}

and use

Map<String,Object> aliasToValueMap = 
        session.createCriteria(User.class)
          .add(Restrictions.idEq(userID))
          .setResultTransformer(new IntrospectClassMetadata())
        .uniqueResult();

Last chance:

Map<String,Object> map = (Map<String,Object>)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...

这篇关于在DB中作为Hibernate中的Map检索一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 00:44