book-project icon indicating copy to clipboard operation
book-project copied to clipboard

Fix JPA error of not being able to find ID

Open knjk04 opened this issue 5 years ago • 12 comments

Describe the bug

updateAuthorAffectsOneRow fails in AuthorTest.

[ERROR] updateAuthorAffectsOneRow  Time elapsed: 0 s  <<< ERROR!
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.karankumar.bookproject.backend.entity.Book with id 25; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.karankumar.bookproject.backend.entity.Book with id 25
        at com.karankumar.bookproject.backend.entity.AuthorTest.updateAuthorAffectsOneRow(AuthorTest.java:73)
Caused by: javax.persistence.EntityNotFoundException: Unable to find com.karankumar.bookproject.backend.entity.Book with id 25
        at com.karankumar.bookproject.backend.entity.AuthorTest.updateAuthorAffectsOneRow(AuthorTest.java:73)

To Reproduce Steps to reproduce the behaviour:

  1. Run mvn clean install

Expected behaviour

No errors. Test passes.

knjk04 avatar Sep 05 '20 10:09 knjk04

Hi Karan, can I work on this? I think both the test cases in AuthorTest needs a fix.

Thanks, Diego

inhundreds avatar Sep 30 '20 08:09 inhundreds

Hi Diego. Sure, that would help a lot! Thanks!

knjk04 avatar Sep 30 '20 12:09 knjk04

An update (sorry but I've been busy lately):

  • the problem is not only in AuthorTest.java but also in BookServiceTest.validBookSaved() if you try to save the same book 2 times it will throw the same exception.

I'm looking to reproduce the same exception in a simpler setting.

Bye, Diego

inhundreds avatar Oct 07 '20 20:10 inhundreds

Thanks for the update, @inhundreds!

knjk04 avatar Oct 11 '20 05:10 knjk04

Hello Karan, the problem in AuthorTest is that the toRead object keeps a reference to a book that is removed with the resetBookService() method.

Fixing this, though, I found that the beRemovedWhenOrphan test is also broken. It assumes that authors without books are removed from the repository but I can't find this implementation anywhere in the code. That test was working by chance.

I suggest to remove it, but let me know what do you think about it.

Thanks, Diego

inhundreds avatar Oct 21 '20 13:10 inhundreds

@inhundreds Thanks for the update, Diego. I'll take a look at this over the weekend.

@schrothbn I think you noticed the same thing?

knjk04 avatar Oct 22 '20 06:10 knjk04

@knjk04 Yes that's true. Actually that test was working on PR #485 because i needed to change the test setup for fixing the test in the ticket. Was done on an now older version of master though, something might've broken it since then.

schrothbn avatar Oct 22 '20 07:10 schrothbn

@inhundreds Have you tried this on master? Perhaps you don't have @schrothbn's change

knjk04 avatar Nov 01 '20 17:11 knjk04

@knjk04 Now it is ok so I think you can close this issue.

inhundreds avatar Nov 02 '20 21:11 inhundreds

@inhundreds Thanks

knjk04 avatar Nov 03 '20 06:11 knjk04

Got this issue again on spring boot build 3.0.0-SNAPSHOT. Currently have a Student Transaction and then linked to transaction class:

@Id @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false, orphanRemoval = true) @JoinColumn(name = "transaction_id", referencedColumnName = "id") @ExcelColumn(title = "Transaction Information", recurse = true) @Validation(required = true, message = "Failed to create transaction") private Transaction transaction;

Unable to find za.co.silversurge.jezportal.models.Transaction with id 302

Transaction in the database : 302,1,2000,0,2022-12-28 20:55:14.000000,0,BiLlbnkzdc,OR306nCN,CASH

JpSilver avatar Jan 16 '23 10:01 JpSilver

Hi @JpSilver, thanks for letting us know you still face this problem.

Unfortunately, I will be archiving this project as I no longer have the time to maintain it.

knjk04 avatar Apr 10 '23 09:04 knjk04