-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Spring ORM SpringBeanContainer when trying to create a bean fails with not found bean definition, and fallbacks to default hibernate bean creation #30683
Comments
Why a counter suffix will be appended to class name? IMO it's a bug that hibernate team should fix. |
Because it's parameterized bean |
We could try to remove the suffix before considering this as a Spring bean name, but I really wonder why we get that name with a suffix there to begin with. Even for a parametermized bean, Hibernate should expose the raw bean name somewhere as well. Is there a reliable indication for the provided bean name to be parameterized, so that we could strip the suffix for our purposes, but only in that specific case? Is this actually what you'd expect SpringBeanContainer to do, nevertheless finding a bean definition with name |
I do not really think the issue is the suffix of the bean name. The issue is that the SpringBeanContainer creates a bean without any definition and then tries to merge the definition or etc, on nonexisting bean. I have forked the private SpringContainedBean<?> createBean(
String name,
Class<?> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer
) {
try {
if (lifecycleOptions.useJpaCompliantCreation()) {
if (this.beanFactory instanceof BeanDefinitionRegistry bdr) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType);
bdr.registerBeanDefinition(name, beanDefinition);
Object bean = this.beanFactory.getBean(name);
return new SpringContainedBean<>(
bean,
beanInstance -> this.beanFactory.destroyBean(
name,
beanInstance
)
);
} else {
throw new IllegalStateException(
"Cannot register bean definition with non-BeanDefinitionRegistry: "
+ this.beanFactory);
}
} else {
return new SpringContainedBean<>(this.beanFactory.getBean(
name,
beanType
));
}
} catch (BeansException ex) { |
If we still going around the suffix stuff here is the logic which causes the suffix addition when using custom types. |
It's not the lack of a bean definition, it's actually the presence of a provided bean name to begin with. We usually get callbacks for the So in your case, it looks like we should essentially ignore the provided bean name and rather delegate to the I guess we could check for the existence of a corresponding bean definition of the provided name and otherwise simply ignore the name. |
I see, this makes more sense right now, as far as I have done the research and some unrelated issues to the same code the suffix is appended because |
I think from my understanding the |
Maybe that's why Hibernate provides a name there indeed, as a key for distinction between different bean instances. We can keep using the name as a key as we usually do there, but in the |
Going through other |
The CDI BeanContainer implementation translates a given bean name to the Thanks for all the digging here, that's very helpful! |
Thanks for the fix! This will help for many stuff like |
This is in |
Affected Version: 5.3.28
There seems to be an issue with Spring ORM
SpringBeanContainer
when Hibernate tries to create a bean using theSpringBeanContainer::createBean
method:https://github.com/spring-projects/spring-framework/blob/main/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java#L183-L185
Upon the creation of a bean, the method
this.beanFactory.applyBeanPropertyValues(bean, name);
is called. This method internally attempts to merge the bean definition. However, the definition does not exist and cannot be created by the developer due to Hibernate appending a counter suffix to the bean name (e.g.,org.example.types.CustomType0
). This causes the entire process to fail with the error message "No bean namedorg.example.types.CustomType0
," ultimately resorting to Hibernate's fallback method for bean creation. This negates the intended purpose of using theSpringBeanContainer
for bean creation.https://github.com/spring-projects/spring-framework/blob/main/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java#L403
A review of the code history suggests that this behavior has remained unchanged for quite some time. However, this approach appears to be flawed and may need to be reconsidered to ensure the proper functioning of the integration between Spring ORM and Hibernate when dealing with the creation of beans.
The text was updated successfully, but these errors were encountered: