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

@Version attribute isn't set as 0 when creating a new entity #608

Open
dyleph opened this issue Apr 12, 2023 · 8 comments
Open

@Version attribute isn't set as 0 when creating a new entity #608

dyleph opened this issue Apr 12, 2023 · 8 comments

Comments

@dyleph
Copy link

dyleph commented Apr 12, 2023

Hi team,

I am upgrading com.vladmihalcea:hibernate-types-55 from 2.17.3 to 2.21.1, and get some failing tests due to @Version attribute value is not as expected.
The test is about creating a new entity and check its version is 0.
But actually the version is 1, which is not as expected.

I tried to test on several versions between 2.17.3 and 2.21.1. It seems that since 2.20.0, the issue starts happening.

Could you please help to check if is there any change about the default value of @Version attribute? Thank you :)

@vladmihalcea
Copy link
Owner

You will need to create a replicating test case to demonstrate the issue. Use the existing test cases as a reference.

I'll check it out afterwards.

@dyleph
Copy link
Author

dyleph commented Apr 14, 2023

Hi @vladmihalcea ,

Thank you for your quick reply. I just figured out which case causes a problem.
I have an entity Books, which defines bookDetails is a nested Map with Publisher keys and another nested Map with BookTypes keys and BookInventory value as below:

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Map<Publisher, Map<BookTypes, BookInventory>> bookDetails;

Here is my test case:
1/ Check if bookId is existed in DB
2a/ If it isn't existed, it will save a new record into DB
2b/ If it is existed, just update a record
3/ Once saving successfully in DB, the test do some assertion to validate.

After finishing a test, the console log shows what Hibernate did:

Hibernate:
select
books0_.id as id1_0_0_,
books0_.book_details as book_det2_0_0_,
books0_.version as version3_0_0_
from
sh_books books0_
where
books0_.id=?
-> This log matches with step 1

Hibernate:
insert
into
sh_books
(book_details, version, id)
values
(?, ?, ?)
-> This log matches with step 2a, because my given book_id is not in the DB

Hibernate:
update
sh_books
set
book_details=?,
version=?
where
id=?
and version=?
-> This is weird. Because we don't want to update anything. It could be the point, which causes the current problem of @Version.

Hibernate:
select
books0_.id as id1_0_0_,
books0_.book_details as book_det2_0_0_,
books0_.version as version3_0_0_
from
sh_books books0_
where
books0_.id=?
-> This matches with step 3

I also try to test another case of Map. By modifying bookDetails as a Map with String keys and Publisher values as below:

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Map<String, Publisher> bookDetails;

Then run a test without any error. Here is the console log shows what Hibernate did:

Hibernate:
select
books0_.id as id1_0_0_,
books0_.book_details as book_det2_0_0_,
books0_.version as version3_0_0_
from
sh_books books0_
where
books0_.id=?
-> This log matches with step 1

Hibernate:
insert
into
sh_books
(book_details, version, id)
values
(?, ?, ?)
-> This log matches with step 2a

Hibernate:
select
books0_.id as id1_0_0_,
books0_.book_details as book_det2_0_0_,
books0_.version as version3_0_0_
from
sh_books books0_
where
books0_.id=?
-> This log matches with step 3

It didn't try to update a new record in DB.

This is a comparison between com.vladmihalcea:hibernate-types-55 2.17.3 and 2.20.0
hibernate-types-parent-2.19.2...hibernate-types-parent-2.20.0
There is some change on Map, which could be related to..

I updated my quick demo on github (https://github.com/dyleph/books-demo/commits/main). Could you please help to take a look?
I really appreciate 😄

@vladmihalcea
Copy link
Owner

vladmihalcea commented Apr 14, 2023

@dyleph I won't have the time to investigate it since we're celebrating Easter, but most likely the problem is caused by the lack of equals and hashCode in all the Objects you store in that Map (keys and values).

@dyleph
Copy link
Author

dyleph commented Apr 14, 2023

Sorry @vladmihalcea about asking you in Easter time.
Not sure if I get your suggestion in wrong way, but I added @EqualsAndHashCode annotation in that Map.
However, we can keep this until you are back. Again, I am sorry and enjoy your holiday. :-)

@vladmihalcea
Copy link
Owner

In the meantime, you need to provide a Pull Request into this project that contains a test case demonstrating the issue. There are test cases that assert the number of executed SQL statements. You need to use those too.

So, you need to adapt your example into this project so that it becomes a test that will run automatically.

@dyleph
Copy link
Author

dyleph commented Apr 19, 2023

Hi @vladmihalcea ,
It seems that I don't have permission to push my testing branch into this project. Then I can create a PR for checking :(

@vladmihalcea
Copy link
Owner

You need to fork the repository and push to your fork.

Then, you can send the Pull Request.

@dyleph
Copy link
Author

dyleph commented Apr 26, 2023

Here is the pull request. I put it here as we can keep track. #619
Thanks @vladmihalcea :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants