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

Enhance direct access to protected, package-private, private and superclass entity fields in Hibernate ORM #32876

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public HibernateEnhancersRegisteredBuildItem enhancerDomainObjects(JpaModelBuild
@BuildStep
public HibernateModelClassCandidatesForFieldAccessBuildItem candidatesForFieldAccess(JpaModelBuildItem jpaModel) {
// Ask Panache to replace direct access to public fields with calls to accessors for all model classes.
return new HibernateModelClassCandidatesForFieldAccessBuildItem(jpaModel.getAllModelClassNames());
return new HibernateModelClassCandidatesForFieldAccessBuildItem(jpaModel.getManagedClassNames());
}

@BuildStep
Expand Down Expand Up @@ -1235,7 +1235,7 @@ private void enhanceEntities(final JpaModelBuildItem jpaModel,
List<AdditionalJpaModelBuildItem> additionalJpaModelBuildItems,
BuildProducer<GeneratedClassBuildItem> additionalClasses) {
HibernateEntityEnhancer hibernateEntityEnhancer = new HibernateEntityEnhancer();
for (String i : jpaModel.getAllModelClassNames()) {
for (String i : jpaModel.getManagedClassNames()) {
transformers.produce(new BytecodeTransformerBuildItem(true, i, hibernateEntityEnhancer, true));
}
for (AdditionalJpaModelBuildItem additionalJpaModel : additionalJpaModelBuildItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public JpaModelBuildItem discoverModelAndRegisterForReflection() {
enlistExplicitMappings(collector, persistenceUnitContribution);
}

Set<String> allModelClassNames = new HashSet<>(collector.entityTypes);
allModelClassNames.addAll(collector.modelTypes);
for (String className : allModelClassNames) {
Set<String> managedClassNames = new HashSet<>(collector.entityTypes);
managedClassNames.addAll(collector.modelTypes);
for (String className : managedClassNames) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(className).methods().fields().build());
}

Expand All @@ -121,12 +121,6 @@ public JpaModelBuildItem discoverModelAndRegisterForReflection() {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(javaType).methods().build());
}

// Converters need to be in the list of model types in order for @Converter#autoApply to work,
// but they don't need reflection enabled.
for (DotName potentialCdiBeanType : collector.potentialCdiBeanTypes) {
allModelClassNames.add(potentialCdiBeanType.toString());
}

if (!collector.unindexedClasses.isEmpty()) {
Set<String> unIgnorableIndexedClasses = collector.unindexedClasses.stream().map(DotName::toString)
.collect(Collectors.toSet());
Expand All @@ -143,8 +137,8 @@ public JpaModelBuildItem discoverModelAndRegisterForReflection() {
}
}

return new JpaModelBuildItem(collector.packages, collector.entityTypes, collector.potentialCdiBeanTypes,
allModelClassNames, collector.xmlMappingsByPU);
return new JpaModelBuildItem(collector.packages, collector.entityTypes, managedClassNames,
collector.potentialCdiBeanTypes, collector.xmlMappingsByPU);
}

private void enlistExplicitMappings(Collector collector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ public final class JpaModelBuildItem extends SimpleBuildItem {

private final Set<String> allModelPackageNames = new TreeSet<>();
private final Set<String> entityClassNames = new TreeSet<>();
private final Set<String> managedClassNames = new TreeSet<>();
private final Set<DotName> potentialCdiBeanClassNames = new TreeSet<>();
private final Set<String> allModelClassNames = new TreeSet<>();
private final Map<String, List<RecordableXmlMapping>> xmlMappingsByPU = new HashMap<>();

public JpaModelBuildItem(Set<String> allModelPackageNames, Set<String> entityClassNames,
Set<DotName> potentialCdiBeanClassNames, Set<String> allModelClassNames,
Set<String> managedClassNames, Set<DotName> potentialCdiBeanClassNames,
Map<String, List<RecordableXmlMapping>> xmlMappingsByPU) {
this.allModelPackageNames.addAll(allModelPackageNames);
this.entityClassNames.addAll(entityClassNames);
this.managedClassNames.addAll(managedClassNames);
this.potentialCdiBeanClassNames.addAll(potentialCdiBeanClassNames);
this.allModelClassNames.addAll(allModelClassNames);
this.allModelClassNames.addAll(managedClassNames);
// Converters need to be in the list of model types in order for @Converter#autoApply to work,
// but they don't need reflection enabled.
for (DotName potentialCdiBeanClassName : potentialCdiBeanClassNames) {
this.allModelClassNames.add(potentialCdiBeanClassName.toString());
}
this.xmlMappingsByPU.putAll(xmlMappingsByPU);
}

Expand All @@ -50,6 +57,13 @@ public Set<String> getEntityClassNames() {
return entityClassNames;
}

/**
* @return the list of managed class names: entities, mapped super classes and embeddables only.
*/
public Set<String> getManagedClassNames() {
return managedClassNames;
}

/**
* @return the list of classes that might be retrieved by Hibernate ORM as CDI beans,
* e.g. converters, listeners, ...
Expand Down
Loading