JPA is returning my entity with ONLY the id populated, all other fields are null

Viewed 984

JPA is returning my entity with ONLY the id populated, all other fields are null (as verified by the debugger). It is fetching the entity by doing a search on another column using a JPA Query, which results in the entity having an id of 13 (which is correct) but the rest of the fields are populated and defined in the db as not null, any ideas as to why this is happening would be great.

[I had a bunch of code listed but as it does not contribute to the answer (well more precisely the question) I removed it.]

2 Answers

I also encounter this problem recently, it looks like spring data does some kind of instantiation.

I just change Cascade Type from MERGE to PERSIST and my issue resolved. As Persist and merge are designed as a way to keep one managed instance of certain object.If you use persist it means the object does not exist yet, so making it a unique managed instance doesn't hurt.

When you use merge, you take into consideration that a managed instance of the object may already exist. You don't wanna replace that unique managed instance, because some other object might reference it, believing it is the managed object.

Related