简体   繁体   English

jpa查询怎么写? 在存储库中

[英]how to write jpa query? in repository

enter image description here this img is user table and example tuple here,在此处输入图像描述此 img 是用户表和示例元组,

I want to use sql query "SELECT MAX(KEY_NAME) FROM USER;"我想使用 sql 查询“SELECT MAX(KEY_NAME) FROM USER;” and right here enter image description here So in java, I wrote jpa query but I met ERROR 16148然后在此处输入图片描述所以在 java 中,我写了 jpa 查询,但遇到错误 16148

2023-02-01 20:58:51.668 ERROR 16148 --- [nio-8080-exec-7] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 2023-02-01 20:58:51.668 错误 16148 --- [nio-8080-exec-7] oac.c.C.[.[.[/].[dispatcherServlet]:servlet [dispatcherServlet] 的 Servlet.service()在路径 [] 的上下文中抛出异常 [请求处理失败; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Type specified for TypedQuery [gdsc.toypj.dutchpayit.domain.User] is incompatible with query return type [class java.lang.Long];嵌套异常是 org.springframework.dao.InvalidDataAccessApiUsageException:为 TypedQuery [gdsc.toypj.dutchpayit.domain.User] 指定的类型与查询返回类型 [class java.lang.Long] 不兼容; nested exception is java.lang.IllegalArgumentException: Type specified for TypedQuery [gdsc.toypj.dutchpayit.domain.User] is incompatible with query return type [class java.lang.Long]] with root cause嵌套异常是 java.lang.IllegalArgumentException:为 TypedQuery [gdsc.toypj.dutchpayit.domain.User] 指定的类型与查询返回类型 [class java.lang.Long]] 不兼容,其根本原因

here my source这是我的来源

UserRepository.java UserRepository.java


public User findOneUser() {
    return em.createQuery("select MAX(r.id) from User r", User.class)
            .getSingleResult();
}

User.java用户.java

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "key_name")
private Long id;

private String name;

@OneToMany(mappedBy = "user",cascade = CascadeType.ALL)
private List<Menu> menuList = new ArrayList<>();

@OneToMany(mappedBy = "user",cascade = CascadeType.ALL)
private List<People> peopleList = new ArrayList<>();

public static User addUser(String name){
    User user = new User();
    user.setName(name);
    return user;
}

UserService.java UserService.java

@Transactional
public User OneUser(){
    User user = userRepository.findOneUser();
    return user;
}

UserController.java UserController.java


@GetMapping("/get/one")
public ResponseEntity getOneUser(){

    User user = userService.OneUser();
    return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse(200,user));

}

I've been trying more than 3 hours..我已经尝试了 3 个多小时..

in UserRepository.java, I tried them and error in everything.在 UserRepository.java 中,我尝试了它们,但都出错了。

em.createQuery("select MAX(r.Key_name) from User r", User.class) em.createQuery("select MAX(r.Key_name) from User r", User.class)

em.createQuery("select id from User", User.class) em.createQuery("从用户中选择 id", User.class)

oh this is worked哦,这是有效的

return em.createQuery("select r from User r", User.class)
.getResultList();

why only "select r" is working I don't know!!!!为什么只有“select r”在工作我不知道!!!!

The error comes from the TypedQuery defined to return the User object.错误来自定义为返回User object 的 TypedQuery。

You can try and change the TypedQuery to Long or create another query that will return the User as an object.您可以尝试将 TypedQuery 更改为Long或创建另一个查询以将用户返回为 object。

You can go with something similar like this:你可以使用类似这样的 go:

select r from User r where r.id = (select MAX(r.id) from User r)

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

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