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

HHH-18679 throw exception when columnValues is null while addKeyColumn #9077

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anaconda875
Copy link

@anaconda875 anaconda875 commented Oct 10, 2024

Currently, the code will throw NPE when annotating the id with @Generated(writable = true) WITHOUT sql attribute
This fix is about throwing a dedicated exception instead of NPE as we can not handle any further.

image
Note: I'm a new contributor, so my code might be bad. Please give me advices/suggestions if the PR is so stupid, I will do my best to fix it. Thank you so much


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-18679

@mbladel
Copy link
Contributor

mbladel commented Oct 10, 2024

Hey @anaconda874, thank you so much for opening the PR and trying to solve this!

The NPE thrown by Hibernate of course is an issue, and while your idea of validating this case and generating a better error message is definitely on the right track (though I would not create a new exception and just use IdentifierGenerationException), I think we have the tools to actually allow @Generated(writable = true) on an identifier property to work.

I was looking at your issue as well and I've created #9078, with this change we should be able to understand when a generated id is writable and read the value from the persisted entity instead of letting the database generate one.

@anaconda875
Copy link
Author

anaconda875 commented Oct 10, 2024

I was looking at your issue as well and I've created #9078, with this change we should be able to understand when a generated id is writable and read the value from the persisted entity instead of letting the database generate one.

Hi @mbladel I did not know about the BeforeExecutionGenerator and @ColumnDefault. I have to find another ticket to practise more

@mbladel
Copy link
Contributor

mbladel commented Oct 10, 2024

Don't worry @anaconda875, your contribution is still very much appreciated - and let's see what the others think.

Note: I used @ColumnDefault in the test just for a quick way to have a database-generated value, realistically one would have some sort of trigger / database function generating the identifier value on the fly.

@anaconda875
Copy link
Author

Note: I used @ColumnDefault in the test just for a quick way to have a database-generated value, realistically one would have some sort of trigger / database function generating the identifier value on the fly.

Hi @mbladel , may I ask a question? If there are "some sort of trigger / database function generating the identifier", then why we need writable = true?
The document of sql says that Determines if the value currently assigned to the annotated property is included in SQL insert and update statements

@mbladel
Copy link
Contributor

mbladel commented Oct 11, 2024

If there are "some sort of trigger / database function generating the identifier", then why we need writable = true

Well, as you said (and the documentation you posted is about the writable flag) the user might choose to persist an entity with a value that was already assigned to the property annotated with @Generated. The same can be true for identifiers, mainly useful for testing where you might want to persist some data with a known id value to deterministically produce a desired outcome.

@anaconda875 anaconda875 force-pushed the HHH-18679 branch 7 times, most recently from 72de5e2 to 67fe6c1 Compare October 16, 2024 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants