我在我的项目中使用 spring jpa 数据表:
https://github.com/darrachequesne/spring-data-jpa-datatables
今天发生了一件奇怪的事情。
我的代码中有以下调用:
DataTablesOutput<Message> messages = datatableMessageRepository.findAll(input, null, spec);
几天前工作正常,规范是将结果限制为指定用户。今天停止运行(截至今天,所有用户的总记录为23条)。
它返回此消息:
DataTables warning: table id=messageTable - javax.persistence.EntityNotFoundException: Unable to find com.mycompany.dao.entity.Message with id 4680
数据库有记录4680。
我打开了跟踪,我在日志中发现了这一点:
[TRACE] 2019-02-11 04:08:56.390 [http-nio-127.0.0.1-8000-exec-5] DefaultLoadEventListener - Loading entity: [com.mycompany.dao.entity.Message#4680]
[TRACE] 2019-02-11 04:08:56.392 [http-nio-127.0.0.1-8000-exec-5] DefaultLoadEventListener - Attempting to resolve: [com.mycompany.dao.entity.Message#4680]
[TRACE] 2019-02-11 04:08:56.397 [http-nio-127.0.0.1-8000-exec-5] DefaultLoadEventListener - Object not resolved in any cache: [com.mycompany.dao.entity.Message#4680]
[TRACE] 2019-02-11 04:08:56.400 [http-nio-127.0.0.1-8000-exec-5] AbstractEntityPersister - Fetching entity: [com.mycompany.dao.entity.Message#4680]
然后是一个很长的查询, 然后是:
[TRACE] 2019-02-11 04:08:56.599 [http-nio-127.0.0.1-8000-exec-5] BasicBinder - binding parameter [1] as [BIGINT] - [4680]
[TRACE] 2019-02-11 04:08:56.603 [http-nio-127.0.0.1-8000-exec-5] AbstractLoadPlanBasedLoader - Bound [2] parameters total
[TRACE] 2019-02-11 04:08:56.827 [http-nio-127.0.0.1-8000-exec-5] ResourceRegistryStandardImpl - Registering result set [com.mysql.cj.jdbc.result.ResultSetImpl@d1d2cc5]
[TRACE] 2019-02-11 04:08:56.832 [http-nio-127.0.0.1-8000-exec-5] ResultSetProcessorImpl - Processing result set
[TRACE] 2019-02-11 04:08:56.836 [http-nio-127.0.0.1-8000-exec-5] ResultSetProcessorImpl - Done processing result set (0 rows)
[TRACE] 2019-02-11 04:08:56.842 [http-nio-127.0.0.1-8000-exec-5] AbstractRowReader - Total objects hydrated: 0
但有趣的是,如果我在数据表存储库的 findAll
调用之前调用 MessageRepository.findAll:
List<Message> all = mr.findByUser(user);
DataTablesOutput<Message> messages = datatableMessageRepository.findAll(input, null, spec);
然后查询就成功了!
那这是为什么呢?我错过了一些配置吗?
谢谢
最佳答案
它会失败,因为 JPA 始终期望列中存在非空值,并且加载期间的实体验证失败。您可能有一个 OneToMany/ManyToOne 连接到此列,这会导致实体加载失败,因为找不到关系的另一端。
关于java - Spring data jpa datatables : javax. persistence.EntityNotFoundException:无法找到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627823/