Java nativeQuery: 使用AliasToEntityMapResultTransformer查询,映射中的列是无序的。

huangapple go评论75阅读模式
英文:

Java nativeQuery: ResultSet with AliasToEntityMapResultTransformer has unordered columns in map

问题

我正在尝试将结果集作为映射获取,并且成功地将其放入了映射中。但是映射键的顺序与选择查询的顺序不同。

请检查以下代码:

String queryString = "SELECT T1.COLUMN_11, T1.COLUMN_12, T2.COLUMN_21, T2.COLUMN_22 FROM MYSCHEMA.TABLE1 T1 INNER JOIN MYSCHEMA.TABLE2 T2 ON T1.SOME_COLUMN = T2.SOME_OTHER_COLUMN";

Query query = entityManager.createNativeQuery(queryString)
                       .unwrap(org.hibernate.query.Query.class)
                       .setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        
List<Map<String, Object>> resultSet = queryEx.getResultList();

现在,按照期望,List<Map<String, Object>> resultSet 应该看起来像下面这样:

[
  {
    "COLUMN_11": "VALUE11",
    "COLUMN_12": "VALUE12",
    "COLUMN_21": "VALUE21",
    "COLUMN_22": "VALUE22"
  },
  ...............
]

但是列的顺序混乱,类似于下面这样:

[
  {
    "COLUMN_21": "VALUE21",
    "COLUMN_12": "VALUE12",
    "COLUMN_11": "VALUE11",
    "COLUMN_22": "VALUE22"
  },
  ...............
]

是否有任何方法可以保留与选择语句中相同的顺序?

注:由于查询是动态的,无法创建一个模型来保存结果集值,所以必须只能使用映射。

英文:

I'm trying to get the resultset as a map and I'm successfully able to get it into a map. But the map keys are not as per the order of select query

Please check the code:

String queryString = &quot;SELECT T1.COLUMN_11, T1.COLUMN_12, T2.COLUMN_21, T2.COLUMN_22 FROM MYSCHEMA.TABLE1 T1 INNER JOIN MYSCHEMA.TABLE2 T2 ON T1.SOME_COLUMN = T2.SOME_OTHER_COLUMN&quot;;

Query query = entityManager.createNativeQuery(queryString)
                       .unwrap(org.hibernate.query.Query.class)
                       .setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        
List&lt;Map&lt;String, Object&gt;&gt; resultSet = queryEx.getResultList();

Now as per expectation the List&lt;Map&lt;String, Object&gt;&gt; resultSet should look something like below

[
  {
    &quot;COLUMN_11&quot;: &quot;VALUE11&quot;,
    &quot;COLUMN_12&quot;: &quot;VALUE12&quot;,
    &quot;COLUMN_21&quot;: &quot;VALUE21&quot;,
    &quot;COLUMN_22&quot;: &quot;VALUE22&quot;
  },
  ...............
]

But the colums are jumbled something like below

[
  {
    &quot;COLUMN_21&quot;: &quot;VALUE21&quot;,
    &quot;COLUMN_12&quot;: &quot;VALUE12&quot;,
    &quot;COLUMN_11&quot;: &quot;VALUE11&quot;,
    &quot;COLUMN_22&quot;: &quot;VALUE22&quot;
  },
  ...............
]

Is there anyway I can retain the order as in select statement ?

> PS: Can't create a model to hold the resultSet values as query is
> dynamic. So "HAD TO" use map only.

答案1

得分: 2

尝试移除<code>.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)</code>。我不是Hibernate专家,但大多数JPA实现默认返回保留列顺序的对象数组列表。

另一种方法是实现自己的转换器,在其中使用LinkedHashMap而不是HashMap。AliasToEntityMapResultTransformer使用HashMap。

英文:

Try to remove <code>.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE)</code>. I'm not an Hibernate expert but most JPA implementation return a list of Object arrays by default that preserve the order of the columns.

An alternative would be to implement your own transformer where you use a LinkedHashMap instead of a HashMap. AliasToEntityMapResultTransformer uses HashMap.

huangapple
  • 本文由 发表于 2020年10月6日 22:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64227878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定