简体   繁体   English

使用 JPA 本机查询和 MemSql 选择 json 列

[英]Select json column using JPA native query with MemSql

I have a table in a MemSql database which contains a column of type JSON .我在MemSql数据库中有一个表,其中包含一个JSON类型的列。

I am trying to execute the following query.我正在尝试执行以下查询。

select tweet from tweets_json;

The tweet column is the JSON column. tweet列是JSON列。

Here is the code I am using to execute this query.这是我用来执行此查询的代码。

public List<String> getTweets(){
    Query q = entityManager.createNativeQuery("select tweet from tweets_json");
    List<String> resultList = query.getResultList();
}

I am expecting the result to be a list of strings and each string to represent the JSON.我期望结果是一个字符串列表和每个字符串来表示 JSON。

The problem is that I am getting the string converted to as a single Character object which contains only the first character of the JSON { .问题是我将字符串转换为单个Character对象,该对象仅包含 JSON {的第一个字符。

The result is always a list of Character with the open curly bracket symbol regardless the type of the list item I am using.无论我使用的列表项是什么类型,结果始终是一个带有左大括号符号的字符列表。

I tried using List<Object[]> , List<String[]> , List<Object> , List<JsonElement> and all returing the same result.我尝试使用List<Object[]>List<String[]>List<Object>List<JsonElement>并且都返回相同的结果。

I even tried to not specify the type of the list elements and return just a List and the result still the same.我什至试图不指定列表元素的类型,只返回一个列表,结果仍然相同。

How can I get the whole JSON and what is the root cause of this issue?如何获取整个 JSON 以及此问题的根本原因是什么?

This is what worked out for me.这对我有用。

Use CAST(tweet as CHAR(1000)) in native sql query for typecasting json在原生 sql 查询中使用CAST(tweet as CHAR(1000))来对 json 进行类型转换

Query q = entityManager.createNativeQuery("select CAST(tweet as CHAR(1000)) from tweets_json");

This will return you char[] array which can be cast to String and can be used further.这将返回char[]数组,该数组可以转换为String并可以进一步使用。

You need to use JSON_EXTRACT_STRING function.您需要使用 JSON_EXTRACT_STRING 函数。

select JSON_EXTRACT_STRING(tweet,0) from tweets_json;

The exact functiona definition is:确切的函​​数定义是:

JSON_EXTRACT_<type>(json, keypath)

You can find examples here: https://docs.memsql.com/sql-reference/v6.7/json_extract_type/您可以在此处找到示例: https : //docs.memsql.com/sql-reference/v6.7/json_extract_type/

and here和这里

Reading JSON from MEMSQL 从 MEMSQL 读取 JSON

With respect to the common case when EntityManager / HibernateSession is used, a custom Hibernate Type is necessary .对于使用EntityManager / HibernateSession的常见情况,需要自定义 Hibernate Type

I'll suggest an alternative approach - you could retrieve the column as a string and thereby not need Hibernate to understand anything about JSON:我将建议一种替代方法 - 您可以将列作为字符串检索,从而不需要 Hibernate 来了解有关 JSON 的任何内容:

Query q = entityManager.createNativeQuery("select tweet :> text from tweets_json");

That's typecasting the JSON field to text , a string datatype (you could also use varchar or whatever as needed).这就是将 JSON 字段类型转换为text ,一种字符串数据类型(您也可以根据需要使用 varchar 或任何类型)。

The result returned is the same (just a string) either way, but this way Hibernate should be able to understand that it's a string.无论哪种方式返回的结果都是相同的(只是一个字符串),但是这样 Hibernate 应该能够理解它是一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM