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

Register Hibernate @EmbeddableInstantiators registered on JPA embeddables for reflection #31534

Closed
odrotbohm opened this issue Nov 1, 2023 · 2 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@odrotbohm
Copy link
Member

Hibernate allows customizing the instantiation of JPA embeddables via an implementation that can be registered via the @EmbeddableInstantiator annotation on a type or field. It will try to instantiate those via their default constructor. It would be nice if Spring Framework's entity processing would automatically generate the necessary reflection metadata for those declarations.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 1, 2023
@sdeleuze sdeleuze self-assigned this Nov 1, 2023
@sdeleuze sdeleuze added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement labels Nov 1, 2023
@sdeleuze sdeleuze added this to the 6.1.x milestone Nov 1, 2023
@snicoll snicoll removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 1, 2023
@sdeleuze sdeleuze modified the milestones: 6.1.x, 6.1.0 Nov 6, 2023
sdeleuze added a commit to sdeleuze/spring-aot-smoke-tests that referenced this issue Nov 7, 2023
sdeleuze added a commit to sdeleuze/spring-aot-smoke-tests that referenced this issue Nov 7, 2023
@odrotbohm
Copy link
Member Author

odrotbohm commented Nov 7, 2023

Thanks for the quick turnaround. Unfortunately, the fix misses the situation that an embeddable type is annotated, but the field then doesn't have to be anymore:

@Embeddable
@EmbeddableInstantiator(…)
class Foo { … }

@Entity
class MyEntity {
  Foo foo;
}

In other words, the processing would need to detect the annotation on a managed type as well.

Looking at the current implementation, I think this can be simplified to scan the managed type, its fields and the types of those fields (field.getType()) for the annotation.

@odrotbohm
Copy link
Member Author

This seems to do the trick for me (replace PersistenceManagedTypesBeanRegistrationAotProcessor.contributeHibernateHints(…) with the following:

private void contributeHibernateHints(RuntimeHints hints, Class<?> managedClass) {

	if (embeddableInstantiatorClass == null) {
		return;
	}

	ReflectionHints reflection = hints.reflection();

	registerInstantiatorForReflection(reflection,
			AnnotationUtils.findAnnotation(managedClass, embeddableInstantiatorClass));

	ReflectionUtils.doWithFields(managedClass, field -> {
		registerInstantiatorForReflection(reflection,
				AnnotationUtils.findAnnotation(field, embeddableInstantiatorClass));
		registerInstantiatorForReflection(reflection,
				AnnotationUtils.findAnnotation(field.getType(), embeddableInstantiatorClass));
	});
}

void registerInstantiatorForReflection(ReflectionHints reflection, Annotation annotation) {

	if (annotation == null) {
		return;
	}

	Class<?> embeddableInstantiatorClass = (Class<?>) AnnotationUtils.getAnnotationAttributes(annotation)
			.get("value");
	reflection.registerType(embeddableInstantiatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}

@sdeleuze sdeleuze reopened this Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants