-
Notifications
You must be signed in to change notification settings - Fork 3k
Register Hibernate GenericGenerator strategies for reflection #1767
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Could you add a test?
Set<String> strategies = new HashSet<>(); | ||
for (AnnotationInstance annotation : index.getIndex().getAnnotations(GENERIC_GENERATOR)) { | ||
String strategy = annotation.value("strategy").asString(); | ||
if (strategy.contains(".")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering: why checking for the dot being contained in the strategy string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am not mistaken, strategy could be a simple string, but maybe I am mixing this with the strategy from GenericValue
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right it could be a special keyword, but also it could be a "fully qualified name" with no package. (That's far fetched but some people do it!)
We also need to deal with the opposite problem: custom identifier generators can be registered with any name, I'm not aware of a rule that prohibits these to have a "." in the name.
sorry I know that's all bad news but that's how it is :)
It might help to peek into org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory#getIdentifierGeneratorClass
.
you could also - instead of registering these for reflection - instantiate the various identifierGenerators , store them in an immutable map, and insert a custom IdentifierGeneratorFactory
implementation in the ORM bootstrap registry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a problem at all, I'll revisit later on tonight or over the weekend if you don't mind :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, thanks a lot!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WRT the second approach IIUC I would have to change this with an Initiator that would return the custom IdentifierGeneratorFactory
, correct?
I'm asking because I haven't looked at the Hibernate part of Quarkus before ( this PR is a great opportunity to do so :) ) and I don't want to stray down a completely incorrect path :P.
// register the strategies of @GenericGenerator for reflection | ||
Set<String> strategies = new HashSet<>(); | ||
for (AnnotationInstance annotation : index.getIndex().getAnnotations(GENERIC_GENERATOR)) { | ||
String strategy = annotation.value("strategy").asString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the asString()
method could hit an NPE ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it couldn't because the strategy
field is necessary for GenericGenerator
, but I guess we can be defensive, it doesn't hurt :)
I will also go ahead and add a test for this |
It looks like my test is not passing locally so I need to dig in more |
Should be good to go now |
@Sanne I added another commit that implements the strategy you propose - to the best of my understanding. There are probably a few things wrong since I am not really familiar with the Hibernate internals :). Update |
thanks @geoand , I'll have a look. |
Looking forward to it :) |
@geoand great work on figuring out how the ORM boostrap works 👍 And I'm sorry I didn't put more comments, there's a lot to do still :) I found a couple little problems:
I'll clean this up, I have a local branch already.. |
Thanks for the explanation @Sanne, it is highly appreciated! I am looking forward to seeing how it all comes together since it's a great opportunity to learn about Hibernate internals 😉 |
closing this one, replaced by #1809 |
Fixes: #1743
Note: I'm not sure if where I added the reflection is the "proper" place or it should be moved to the
JpaJandexScavenger
class for example.