Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity updates not persisted when using the repository pattern #6945

Closed
danielpetisme opened this issue Feb 2, 2020 · 3 comments · Fixed by #6923
Closed

Entity updates not persisted when using the repository pattern #6945

danielpetisme opened this issue Feb 2, 2020 · 3 comments · Fixed by #6923
Assignees
Labels
kind/bug Something isn't working
Milestone

Comments

@danielpetisme
Copy link
Contributor

Describe the bug
When using the repository pattern, the entity changes are not commited to the database.

Expected behavior
The entity should be managed thus changes tracked and persisted to the DB.

Actual behavior

To Reproduce
Steps to reproduce the behavior:

  1. Clone https://github.com/danielpetisme/quarkus-issue-changes-not-commited-with-repository
  2. mvn compile test

You'll find 2 entities:

  • Fruit standard entity
  • Person a Panache entity

Both entities have a REST Resource layer and a Test to verify the creation and the update.
For the Fruit I use the repository pattern and for the Person the panache facilities.

The result of the tests are:

  • FruitResourceTest#testCreateFruit: ✅

  • FruitResourceTest#testUpdateFruit: ❌

  • PersonResourceTest#testCreatePerson: ✅

  • PersonResourceTest#testUpdatePerson: ✅

Configuration

# Configuration file
# key = value
quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:test
quarkus.datasource.driver=org.h2.Driver
quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.sql-load-script=no-file
quarkus.hibernate-orm.database.generation=none

quarkus.flyway.migrate-at-start=true

Screenshots
(If applicable, add screenshots to help explain your problem.)

Environment (please complete the following information):

  • Output of uname -a or ver:
  • Output of java -version:
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.4+11, mixed mode)
  • GraalVM version (if different from Java):
  • Quarkus version or git rev: 1.2.0.Final

Additional context
(Add any other context about the problem here.)

@danielpetisme danielpetisme added the kind/bug Something isn't working label Feb 2, 2020
@loicmathieu
Copy link
Contributor

@danielpetisme Fruit is a standard entity so you should add getters/setters for it as Quarkus only generates getters/setters for classes that extends PanacheEntity and PanacheEntityBase.

An other solution is to tell hibernate to use the field via @Entity(access = AccessType.FIELD).

@danielpetisme
Copy link
Contributor Author

I confirm using getters/setters do the job with Repository. I have to admit I have been naive on this one..
Now I can see the 2 lines in https://quarkus.io/guides/hibernate-orm-panache#the-daorepository-option

So if Repositories are your thing, you can keep doing them. Even with repositories, you can keep your entities as subclasses of PanacheEntity in order to get the ID and public fields working, but you can even skip that and go back to specifying your ID and using getters and setters if that’s your thing. Use what works for you.

My 2cts, it would worth to add a classic POJO before the repository snippet with a comment like "if the entity does not inherit PanacheEntity you must define getter/setters"
And convert the 2 above line in a Note or Warning section to emphase this point.
WDYT?

Note: I tested the AccessType.Field technic and it seems it does not work :/
I propose to close the issue, thanks @loicmathieu

@loicmathieu
Copy link
Contributor

@danielpetisme I review the documentation of Hibernate with Panache in the following PR : #6923
I agree with you, I will make it more explicit that if an entity didn't inherit from PanacheEntity it needs getters/setters.

It's strange that AccessType.Field didn't works but I'm not an Hibernate expert ...

Let's keep this one open untill the rework on the documentation is done.

@loicmathieu loicmathieu self-assigned this Feb 4, 2020
loicmathieu added a commit to loicmathieu/quarkus that referenced this issue Feb 4, 2020
loicmathieu added a commit to loicmathieu/quarkus that referenced this issue Feb 10, 2020
loicmathieu added a commit to loicmathieu/quarkus that referenced this issue Feb 26, 2020
Fixes quarkusio#6135 and quarkusio#6945
WIP Apply suggestions from code review

will be squashed later

Co-Authored-By: Georgios Andrianakis <[email protected]>
@gsmet gsmet added this to the 1.3.0 milestone Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants