tom.lee
tom.lee

Reputation: 1

spring data jpa CrudRepository findone sometimes return null

My question description:

1 my userDao has two method one calls repository.saveAndFlush, the other calls repository.findone then update record , these two methods are called in different transactions, when first transaction is finished then start to run methd 2, but sometimes the method 2 repository.findone return null.

2 environment: mysql ,springdatajpa use hibernate

3 can anyone give me some solutions, many thanks..

Upvotes: 0

Views: 613

Answers (1)

hizmarck
hizmarck

Reputation: 736

in my case I had a similar incidence, I use jhipster:

"generator-jhipster": "4.6.0", Hiberante 5.2.8.Final.

It happened to me that after saving and reloading the edit view, for example http: // localhost / entity / {id} I got EntityNotFoundException, I thought it was due to some configuration of hibernate with its second level cache, but after reviewing my entity I realized that I had this:

@ManyToOne (optional = false)
     private Order originOrder;

@ManyToOne (optional = false)
     private WhareHouse deliveryLocation;

When originOrder and deliveryLocation could be null, then change them as my business model should be:

@ManyToOne (optional = true)
     private Order originOrder;

@ManyToOne (optional = true)
     private WhareHouse deliveryLocation;

And what do you think? That was all, it had taken me more than 2 weeks reading and looking for references to this weird error, I hope and this could help you, if that is the case

Upvotes: 1

Related Questions