darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
450 stars 172 forks source link

Spring data jpa datatables: javax.persistence.EntityNotFoundException: Unable to find entity #98

Closed Mike-the-one closed 4 years ago

Mike-the-one commented 5 years ago

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?

And this is my datatableMessageRepository class:

@Repository
public interface DatatableMessageRepository extends DataTablesRepository<Message, Long> {

}

I have other datatablerepository classes, they also do not override any method (similar to above) and they seem to work fine.

thanks

Mike-the-one commented 5 years ago

Found the problem.

The table row has a field which referrers to another table, db schema is set to nullable, but entity class is set to nullable=false. Changed to nullable=true then it worked. I don't know why... so if anyone knows why this caused the issue, please help me to understand :)

darrachequesne commented 4 years ago

Closed due to inactivity, please reopen if needed.