[英]Use JSON_EXTRACT to get value from JSON in Spring JPA Custom Query for custom DTO
I want to get value from the JSON column and return custom DTO in spring JPA.我想从 JSON 列中获取值并在 spring JPA 中返回自定义 DTO。
Table structure表结构
userId (int)
name (string)
street (string)
zipcode (string)
state (string)
country (string)
meta (JSON)
meta
column contains age for example {"age": "45"}
meta
列包含年龄,例如{"age": "45"}
I want to fetch a list of users having id
, name
, and age
.我想获取具有
id
、 name
和age
的用户列表。 As the data can be huge I created a custom DTO UserDataDto
由于数据可能很大,我创建了一个自定义 DTO
UserDataDto
Below is an example of the same:以下是相同的示例:
@Query("SELECT new com.model.UserDataDto(userId, name, FUNCTION('JSON_EXTRACT', meta, '$.age')) " +
"FROM User " +
"WHERE userId IN (:userIds) ")
List<UserDataDto> findUsersByIdIn(@Param("userIds") List<Long> userIds);
User
Entity: User
实体:
@Data
@Table(name = "user")
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
private String name;
private String street;
private String zipcode;
private String state;
private String country;
@NotNull
@Convert(converter = UserMetaConverter.class)
private UserMeta meta;
}
UserMeta
structure: UserMeta
结构:
@Data
public class UserMeta {
private Integer age;
}
UserDataDto
structure: UserDataDto
结构:
public class UserDataDto {
private Long userId;
private String name;
private Integer age;
}
On starting the spring boot application I'm getting在启动 spring 启动应用程序时,我得到
WARN |
警告 | Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepo': Invocation of init method failed;
上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepo': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.db.UserRepo.findUsersByIdIn(List<java.lang.Long>)!
嵌套异常是 java.lang.IllegalArgumentException:查询方法 public abstract java.util.List com.db.UserRepo.findUsersByIdIn(List<java.lang.Long>) 的查询验证失败!
The only solution that I can come up with is using a Native query, any other solution is highly appreciated.我能想出的唯一解决方案是使用本机查询,非常感谢任何其他解决方案。
The only solution I can come up with is using @NamedNativeQuery
and @SqlResultSetMapping
我能想出的唯一解决方案是使用
@NamedNativeQuery
和@SqlResultSetMapping
in User
Entity在
User
实体中
@Data
@Table(name = "user")
@Entity
@NamedNativeQuery(
name = "findUsersByIdIn",
query = "SELECT userId, name, meta->>'$.age' as age " +
"FROM user " +
"WHERE user_id = :userIds",
resultSetMapping = "UserDataDto"
)
@SqlResultSetMapping(
name = "UserDataDto",
classes = @ConstructorResult(
targetClass = UserDataDto.class,
columns = {
@ColumnResult(name = "userId", type = Long.class),
@ColumnResult(name = "name", type = String.class),
@ColumnResult(name = "age", type = Integer.class)
}
)
)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String street;
private String zipcode;
private String state;
private String country;
@NotNull
@Convert(converter = UserMetaConverter.class)
private UserMeta meta;
}
in Repository在存储库中
public interface UserRepo extends JpaRepository<User, Long> {
@Query("findUsersByIdIn", nativeQuery = true)
List<UserDataDto> findUsersByIdIn(@Param("userIds") List<Long> userIds);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.