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
@Inject Injection into EntityListener not working #6948
Comments
Support for JPA Listeners was not implemented yet; please see the limitations documented here: @HonoluluHenk , may I re-classify this report as a feature request? |
OMG, sorry for missing this :(
sure! |
For all those arriving here via google: A workaround is to create some other bean and @Inject your bean there... just so Quarkus knows it needs to register your bean for CDI injection.
Then retrieve your bean instance in the EntityListener like this: |
no worries at all :) I realize it's not very visible; also we could do a better job in logging a warning. |
I have the same use case -> so +1 for the "user demand" |
See pr #7296 for possible fix/workaround |
@HonoluluHenk thanks for your contribution. However, I think that we should avoid adding the It should be fairly easy to identify entity listerners (i.e. scan the index for Ideally, Hibernate should provide an SPI that can be implemented by quarkus. If I understand it correctly, Hibernate is currently using @Sanne CC |
I'll have a look into it... |
@mkouba +1 no need for fake classes. Regarding the Hibernate SPI: to be honest I don't know this area very well, I should investigate but am a bit overwhelmed ATM. Even if it's not possible today, it's certainly possible to change the code :) We do regular Hibernate releases to address on any Quarkus need. @HonoluluHenk thanks! appreciate it. If you need to change any code in Hibernate ORM, feel free to take that in consideration. Ping me if you send a PR to Hibernate, I'll prioritize it. (hopefully it won't be needed?) |
@HonoluluHenk Did you get any time to look into this? I am also working on a project which would require support for entity listeners / JPA callbacks. @Sanne Any idea when the Quarkus team will get around to look into this? Possibly we need to consider finding a workaround or contributing a solution. |
sorry, I don't know. It's for sure high on the wish list, but there's a couple more important things that need to be done first and it's a small team. A contribution would be great, or even if you find a reasonable workaround please describe it here for others. |
@Sanne Thanks for the heads-up. |
@knutwannheden For completness: the workaround from #6948 (comment) works fine for me. As a side-note: you need to inject your bean somewhere (it seems to neither matter where, nor if it is actually used there) for it to be available for programmatic lookup via |
Oops. I mixed this issue up with the issue #8268 I recently reported. Please disregard the comment above. |
@knutwannheden that is weird indeed. I am using the workaround in an envers
I'm not sure whether the Btw, how does the error manifest (Build error, NullPointer on runtime, ...)? |
@j-be Using The problem is then that the |
@knutwannheden sorry, my bad, only saw your "Oops" comment just now. Nevermind then, I didn't get |
I'm a little bit confused about what should work and what shouldn't. Regarding to https://quarkus.io/guides/hibernate-orm#limitations, there is no support for JPA Calllbacks. But with 1.3.2.Final, listeners itself (e.g. a method in a Am I missing something? Is the limitation only true in native mode? |
That is also what my testing confirms.
Indeed. In native mode |
public class JpaEntityListeners {
@PrePersist
public void prePersist(Object entity) {
ReflectUtil.invoke(entity, "setCreateTime", LocalDateTime.now());
ReflectUtil.invoke(entity, "setUpdateTime", LocalDateTime.now());
JsonWebToken context = CDI.current().select(JsonWebToken.class).get();
if (Objects.nonNull(context)) {
String username = context.getClaim(Claims.preferred_username.name());
if (Objects.nonNull(username)) {
ReflectUtil.invoke(entity, "setCreateBy", username);
ReflectUtil.invoke(entity, "setUpdateBy", username);
}
}
}
@PreUpdate
public void preUpdate(Object entity) {
ReflectUtil.invoke(entity, "setUpdateTime", LocalDateTime.now());
JsonWebToken context = CDI.current().select(JsonWebToken.class).get();
if (Objects.nonNull(context)) {
String username = context.getClaim(Claims.preferred_username.name());
if (Objects.nonNull(username)) {
ReflectUtil.invoke(entity, "setUpdateBy", username);
}
}
}
} I use |
@HonoluluHenk Have you ever gotten round to looking into this? |
Unfortunately, no. |
I have the same use case -> so +1 for the "user demand" |
Thank you so much, you saved me 24 hours searching for injecting Microprofile JsonWebToken to EntityListeners |
I'm not quite sure what the original problem is but if the |
Dear @SoftMeng |
@eliaharm I think you still need to I think Quarkus is like "you don't use the bean? I'll not pack it then" on buildtime, and |
+1 |
I am facing the same issue. What is currently the easiest way to inject a bean into my Entitylistener (that will also work in native mode) ? As most users here, I want to automatically update the created_by / updated_by column of my entities. |
@TheParad0X |
Fixed by #20708 |
Is this actually fixed? I am using Quarkus 2.7.1 and wrote an entity listener with |
CDI injection in entity listeners does work, at least in the general case: https://github.com/quarkusio/quarkus/pull/20708/files#diff-7ed082417d8730c92d0dcf5c59e547878a5c298311afbd529f1a5c9591f150ccR73 I suppose injecting an entity manager into a component that is itself used by the entity manager could lead to cyclic dependency problems. If you do have problems, please open an issue with a reproducer, and we'll have a look. |
Describe the bug
I do have JPA entities annotated with @EntityListeners.
I @Inject some CDI bean (@RequestScoped in my case but happens for other scopes, too) into the EntityListener.
The bean never gets injected, the reference is allways null.
Things I tried:
Expected behavior
Instance of the CDI bean should get injected
Actual behavior
The CDI bean reference is allways null resulting in a NPE when accessing some attribute of the bean.
To Reproduce
See attached test case
Configuration
(Also happens with postgresql, drop-and-create is just for testing)
Environment (please complete the following information):
java -version
: from 8 to 11Additional context
Interestingly, when the CDI bean I want injected into the EntityListener ist injected somewhere else (e.g. some @ApplicationScoped bean), injection works.
Possible causse: looks like Quarkus bean discovery does not regard EntityListeners as trigger for registering some bean during compile-time-initialization.
Example code:
src/main/java/...
src/test/java/...
The text was updated successfully, but these errors were encountered: