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

Hibernate - @ManyToOne strange behavior #4377

Closed
rafaelkloss opened this issue Oct 4, 2019 · 33 comments · Fixed by #5495
Closed

Hibernate - @ManyToOne strange behavior #4377

rafaelkloss opened this issue Oct 4, 2019 · 33 comments · Fixed by #5495
Assignees
Labels
area/hibernate-orm Hibernate ORM area/persistence OBSOLETE, DO NOT USE kind/bug Something isn't working
Milestone

Comments

@rafaelkloss
Copy link

Describe the bug
Imagine the relationship described below:
"one" User -> "many" Message
In Message class, there is @manytoone(fetch = FetchType.LAZY, optional = false) User user; annotation.
When a new Message instance are created, we need to persist the object on database with related User "id".
I find the related User using EntityManager.find(clazz, id)

Expected behavior
Insert the new Message on database, do not make any operation with User

Actual behavior
After insert Message, Hibernate tries to update the User instance without any field.

To Reproduce
Steps to reproduce the behavior:

  1. Create a User instance on database and persist.
  2. In other transaction, create a Message and set the existent User
  3. Persist the Message

Configuration

# We are using Postgres Database

Can reproduce issue only in quarkus 0.23x, on 0.22 works fine with same config.

@rafaelkloss rafaelkloss added the kind/bug Something isn't working label Oct 4, 2019
@geoand
Copy link
Contributor

geoand commented Oct 4, 2019

cc @Sanne

@gsmet
Copy link
Member

gsmet commented Oct 4, 2019

@rafaelkloss could you create a simple project to reproduce it? It would be really helpful.

@gsmet gsmet added the area/persistence OBSOLETE, DO NOT USE label Oct 4, 2019
@rafaelkloss
Copy link
Author

rafaelkloss commented Oct 8, 2019 via email

@rafaelkloss
Copy link
Author

i'm trying to reproduce the issue using "hibernate-orm-resteasy" project, but i cant :(
Any other suggestion ?

@rafaelkloss
Copy link
Author

HI Guys, there are news about the issue:
For example, in this relationship:
Class 'Solicitacao' have a relationship below with 'Usuario'
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "idUsuarioSolicitacao") private Usuario usuarioSolicitacao;
Class 'Usuario' have relationship below with 'Cargo'
@ManyToOne(optional = true, fetch = FetchType.LAZY) @JoinColumn(name = "idCargo") private Cargo cargo;
Class 'Cargo' have a relationship below with 'Cliente'
@ManyToOne(optional = false) @JoinColumn(name = "idCliente") private Cliente cliente;
When we try to persist a new Solicitacao (first class above), see the difference between 0.22.0 and 0.23.2 qurkus versions:
Screen Shot 2019-10-09 at 2 50 05 PM
Screen Shot 2019-10-09 at 2 53 53 PM
In the 0.22.0, threre is the 'Cliente' instance, but in 0.23.2 the 'Cliente' instance is null and hibernate tries to update 'Cargo' instance with a null 'Cliente' instance causing SQL constraint exception.

@Sanne
Copy link
Member

Sanne commented Oct 9, 2019

Hi @rafaelkloss , if you could share a little project reproduing it we will debug it.

Alternatively, could you try building a snapshot from Hibernate ORM master and try that?
We already fixed a related issue, so I'm wondering if it's the same (or not ..)

@rafaelkloss
Copy link
Author

Hi @Sanne, what is the related issue ?

@gsmet
Copy link
Member

gsmet commented Oct 15, 2019

@rafaelkloss any progress on the reproducer? We really need it to be able to fix that one. And it really looks like a nasty bug so the sooner the better!

Thanks!

@gsmet
Copy link
Member

gsmet commented Oct 15, 2019

BTW, the issue Sanne was talking about is this one: https://hibernate.atlassian.net/browse/HHH-13653 . It has been fixed in the current master of Hibernate ORM but we do not know if it's the same bug or not.

@dreab8
Copy link
Contributor

dreab8 commented Oct 16, 2019

Hi @rafaelkloss,
I have created a branch https://github.com/dreab8/qurkus-merge-detached/tree/issue-4377 on my repo with a test ManyToOneTest#testRequestHasAnAssociatedClient() trying to reproduce the issue you described but without any success, can you please tell me if I missed anything?

Thanks

@rafaelkloss
Copy link
Author

rafaelkloss commented Oct 16, 2019 via email

@rafaelkloss
Copy link
Author

HI guys, i updated to Quarkus 0.25.0, but now there are other issue before described above.
[org.hib.pro.poj.byt.ByteBuddyInterceptor] (executor-thread-1) HHH000179: Narrowing proxy when i find the Company of User.
I suspect there are any problem with my (generic) .equals or hashcode methods in my "abstract model" persistent class.
Any other way for me to go and find out the root cause of the issue?

@dreab8 about your class model, there is a 'AbstractModel' with generic equals and hashCode methods like below:
`public abstract Integer getId();

@Override
public boolean equals(Object o) {
	if (this == o) {
		return true;
	}
	if (o == null || getClass() != o.getClass()) {
		return false;
	}
	AbstractModel abs = (AbstractModel) o;
	if (null == getId() || null == abs.getId()) {
		return false;
	}
	return Objects.equals(getId(), abs.getId());
}

@Override
public int hashCode() {
	return Objects.hash(getId());
}`

@rafaelkloss
Copy link
Author

Do you think the two problems are linked to each other?
For reproduce the main error in this issue, i need to back to Quarkus 0.23.2

@dreab8
Copy link
Contributor

dreab8 commented Oct 17, 2019

Hi @rafaelkloss can you please modify my project in order to reproduce the last issue you described? Thanks

@rafaelkloss
Copy link
Author

Hi @dreab8 i'm trying to do this right now

@rafaelkloss
Copy link
Author

Hi @dreab8 i have good news, take a look on this repository https://github.com/rafaelkloss/quarkus-many-to-one-error
I "destructed" my own project and keep the architecture for reproduce.

@rafaelkloss
Copy link
Author

When you debug, make a breakpoint on ErrorHereServiceImpl line 30

@rafaelkloss
Copy link
Author

I did a fast class diagram to demonstrate my model
Screen Shot 2019-10-25 at 10 34 27 AM

@dreab8
Copy link
Contributor

dreab8 commented Oct 28, 2019

@rafaelkloss thanks a lot for the reproducer, I'll look at it.

@Sanne
Copy link
Member

Sanne commented Oct 28, 2019

A note from @dreab8 : this issue (also) seems related with self-dirty-tracking being enabled during build time enhancement.

@rafaelkloss
Copy link
Author

Hi guys, thanks for the effort, so far we are using 0.22.0.

@rafaelkloss
Copy link
Author

There are any workaround for this ?

@Sanne
Copy link
Member

Sanne commented Oct 30, 2019

a valid workaround seems to be to disable auto dirty tracking, but Quarkus currently enforces it. (That was my choice, as having too many such options makes it hard for us to maintain all possiblities)

Patching Quarkus to allow to disable this is simple enough but it's not a good idea as it would require different code being generated by Panache.

Feel free to experiment with this pr?

The proper solution likely belongs in an ORM (I think, I haven't spoken with @dreab8 yet) but this will need a bit more time.

@rafaelkloss
Copy link
Author

Hi Sanne, thanks for reply, i wait for the finish of issue.
@dreab8 you did it reproduce using my project example ?

@rafaelkloss
Copy link
Author

Hi guys, even i don't understand why works fine until quarkus 0.22.0, before 0.23 the "auto dirty tracking" is disabled ?

@rafaelkloss
Copy link
Author

Hi guys, any update on this ?

@rafaelkloss
Copy link
Author

Hi guys, i'm really preoccupied with this issue. What more i can do to solve this ?
Thanks for understanding

@Sanne
Copy link
Member

Sanne commented Nov 11, 2019

hey @rafaelkloss , I'm sorry for the delay - we had several complex issues to analyse and it's a small team :)

I've been tempted to disable dirty tracking by default, but it would have been only a temporary workaround - and it would have had consequences on other features - so decided to not do that, but rather we should get to the bottom of this.

We'll get back to this soon.

@Sanne
Copy link
Member

Sanne commented Nov 11, 2019

@rafaelkloss there's a PR for your issue here: hibernate/hibernate-orm#3092

It's going to need careful reviewing though. @dreab8 added your use case among the integration tests, but if you'd like to have a look anyway feel free to experiment with it!

@dreab8
Copy link
Contributor

dreab8 commented Nov 11, 2019

@rafaelkloss
Copy link
Author

Thanks :)

@Sanne Sanne added the area/hibernate-orm Hibernate ORM label Nov 14, 2019
@gsmet gsmet added this to the 1.1.0 milestone Nov 15, 2019
@Sanne
Copy link
Member

Sanne commented Nov 15, 2019

hi @rafaelkloss we merged the Hibernate ORM upgrade, this should be fixed now thanks to @dreab8 .

Since he ported your test to be incorporated into the ORM integration tests, I'm closing this one but please could you check on your original case as well :)

@gsmet gsmet removed the backport? label Nov 15, 2019
@gsmet gsmet modified the milestones: 1.1.0, 1.0.0.Final Nov 15, 2019
@rafaelkloss
Copy link
Author

rafaelkloss commented Nov 18, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM area/persistence OBSOLETE, DO NOT USE kind/bug Something isn't working
Projects
None yet
5 participants