Reputation: 425
I am using spring jpa datatable in my project:
https://github.com/darrachequesne/spring-data-jpa-datatables
A strange thing happened today.
I have the following call in my code:
DataTablesOutput<Message> messages = datatableMessageRepository.findAll(input, null, spec);
It was working fine a couple days ago, the spec is it to limit the result to the specified user. It stopped working today (as of today, the total records is 23 for all users).
It returns this message:
DataTables warning: table id=messageTable - javax.persistence.EntityNotFoundException: Unable to find com.mycompany.dao.entity.Message with id 4680
The database has record 4680.
I turned on trace, I found this in the log:
[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]
then a long query, and followed by:
[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
But interesting thing is, if I call a MessageRepository.findAll before datatable repository's findAll
call:
List<Message> all = mr.findByUser(user);
DataTablesOutput<Message> messages = datatableMessageRepository.findAll(input, null, spec);
then the query worked!
So why is that? did I miss config something?
thanks
Upvotes: 1
Views: 1456
Reputation: 802
I got similar issue and in my case it caused by relationship between 2 entities.
Fixed by updating @ManyToOne
annotation at child entity to:
@JoinColumn(nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private ParentEntityClass parent;
and, in addition to this @NotNull
annotation above (probably not necessary)
Upvotes: 0
Reputation: 50
It fails because JPA always expects a not null value in the column and the entity validation during the load fails. You might have a OneToMany / ManyToOne connected to this column and which causes the entity failing to load as the other end of relationship is not found.
Upvotes: 1
Reputation: 425
OK, figured out the problem. The row has a column which is a foreign key of another table, in db schema, it is set to nullable, but in my entity class, it is set to nullable=false. Changed to nullable=true and it worked. But I don't understand why? Can anyone please explain it to me?
thanks
Upvotes: 0