Criteria Builder:如何为 JSON_OBJECT 使用标签
Criteria Builder: How to use label for JSON_OBJECT
我有一个要求,需要在运行时使用条件生成器创建 jsonobject。让我知道如何实现相同的目标。我使用 Mysql 作为数据库。
预期sql: select project_id, json_arrayagg(json_object('name', project_name, 'description', description)) from user_projects group by project_id
到目前为止的代码..
CriteriaQuery<Tuple> criteriaQuery = cBuilder.createTupleQuery();
Root<UserProjects> userProjectsRoot = criteriaQuery.from(UserProjects.class);
// NOT SURE HOW TO INSERT LABELS i.e., 'name' and 'description' in json_object function..
criteriaQuery.multiselect(userProjectsRoot.get("projectId").alias("Id"),
cBuilder.function("json_arrayagg", String.class,
cBuilder.function("json_object", String.class,
userProjectsRoot.get("projectName"),
userProjectsRoot.get("description"))).alias("json"));
criteriaQuery.groupBy(userProjectsRoot.get("projectId"));
Any help would be appreciated.
显然,在您的 SQL 中,标签只是字符串文字,所以这里也只使用字符串文字。
criteriaQuery.multiselect(userProjectsRoot.get("projectId").alias("Id"),
cBuilder.function("json_arrayagg", String.class,
cBuilder.function("json_object", String.class,
cBuilder.literal("name"), userProjectsRoot.get("projectName"),
cBuilder.literal("description"), userProjectsRoot.get("description"))).alias("json"));
我有一个要求,需要在运行时使用条件生成器创建 jsonobject。让我知道如何实现相同的目标。我使用 Mysql 作为数据库。
预期sql: select project_id, json_arrayagg(json_object('name', project_name, 'description', description)) from user_projects group by project_id
到目前为止的代码..
CriteriaQuery<Tuple> criteriaQuery = cBuilder.createTupleQuery();
Root<UserProjects> userProjectsRoot = criteriaQuery.from(UserProjects.class);
// NOT SURE HOW TO INSERT LABELS i.e., 'name' and 'description' in json_object function..
criteriaQuery.multiselect(userProjectsRoot.get("projectId").alias("Id"),
cBuilder.function("json_arrayagg", String.class,
cBuilder.function("json_object", String.class,
userProjectsRoot.get("projectName"),
userProjectsRoot.get("description"))).alias("json"));
criteriaQuery.groupBy(userProjectsRoot.get("projectId"));
Any help would be appreciated.
显然,在您的 SQL 中,标签只是字符串文字,所以这里也只使用字符串文字。
criteriaQuery.multiselect(userProjectsRoot.get("projectId").alias("Id"),
cBuilder.function("json_arrayagg", String.class,
cBuilder.function("json_object", String.class,
cBuilder.literal("name"), userProjectsRoot.get("projectName"),
cBuilder.literal("description"), userProjectsRoot.get("description"))).alias("json"));