Skip to content
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
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/hibernate-orm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,6 @@ The team believes this isn't a big deal as it's trivial to inject the Session vi
handling the binding into a ThreadLocal yourself, making this a legacy feature.
This limitation might be resolved in the future, if someone opens a ticket for it and provides a reasonable use case to justify the need.

JPA Callbacks::
Annotations allowing for application callbacks on entity lifecycle events defined by JPA such as `@javax.persistence.PostUpdate`, `@javax.persistence.PostLoad`, `@javax.persistence.PostPersist`, etc... are currently not processed.
This limitation could be resolved in a future version, depending on user demand.

=== Other notable differences

Format of `import.sql`::
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.hibernate.orm.deployment;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.jboss.jandex.DotName;
Expand All @@ -11,7 +9,7 @@ public final class HibernateOrmAnnotations {
private HibernateOrmAnnotations() {
}

public static final List<DotName> PACKAGE_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
public static final List<DotName> PACKAGE_ANNOTATIONS = List.of(
DotName.createSimple("org.hibernate.annotations.AnyMetaDef"),
DotName.createSimple("org.hibernate.annotations.AnyMetaDefs"),
DotName.createSimple("org.hibernate.annotations.FetchProfile"),
Expand All @@ -27,9 +25,9 @@ private HibernateOrmAnnotations() {
DotName.createSimple("org.hibernate.annotations.NamedQueries"),
DotName.createSimple("org.hibernate.annotations.NamedQuery"),
DotName.createSimple("org.hibernate.annotations.TypeDef"),
DotName.createSimple("org.hibernate.annotations.TypeDefs")));
DotName.createSimple("org.hibernate.annotations.TypeDefs"));

public static final List<DotName> JPA_MAPPING_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
public static final List<DotName> JPA_MAPPING_ANNOTATIONS = List.of(
DotName.createSimple("javax.persistence.Access"),
DotName.createSimple("javax.persistence.AssociationOverride"),
DotName.createSimple("javax.persistence.AssociationOverrides"),
Expand Down Expand Up @@ -120,9 +118,9 @@ private HibernateOrmAnnotations() {
DotName.createSimple("javax.persistence.Temporal"),
DotName.createSimple("javax.persistence.Transient"),
DotName.createSimple("javax.persistence.UniqueConstraint"),
DotName.createSimple("javax.persistence.Version")));
DotName.createSimple("javax.persistence.Version"));

public static final List<DotName> HIBERNATE_MAPPING_ANNOTATIONS = Collections.unmodifiableList(Arrays.asList(
public static final List<DotName> HIBERNATE_MAPPING_ANNOTATIONS = List.of(
DotName.createSimple("org.hibernate.annotations.AccessType"),
DotName.createSimple("org.hibernate.annotations.Any"),
DotName.createSimple("org.hibernate.annotations.AnyMetaDef"),
Expand Down Expand Up @@ -216,10 +214,19 @@ private HibernateOrmAnnotations() {
DotName.createSimple("org.hibernate.annotations.UpdateTimestamp"),
DotName.createSimple("org.hibernate.annotations.ValueGenerationType"),
DotName.createSimple("org.hibernate.annotations.Where"),
DotName.createSimple("org.hibernate.annotations.WhereJoinTable")));
DotName.createSimple("org.hibernate.annotations.WhereJoinTable"));

public static final List<DotName> ANNOTATED_WITH_INJECT_SERVICE = Collections.unmodifiableList(Arrays.asList(
public static final List<DotName> ANNOTATED_WITH_INJECT_SERVICE = List.of(
DotName.createSimple("org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl"),
DotName.createSimple("org.hibernate.engine.jdbc.cursor.internal.StandardRefCursorSupport")));
DotName.createSimple("org.hibernate.engine.jdbc.cursor.internal.StandardRefCursorSupport"));

public static final List<DotName> JPA_LISTENER_ANNOTATIONS = List.of(
DotName.createSimple("javax.persistence.PostLoad"),
DotName.createSimple("javax.persistence.PostPersist"),
DotName.createSimple("javax.persistence.PostRemove"),
DotName.createSimple("javax.persistence.PostUpdate"),
DotName.createSimple("javax.persistence.PrePersist"),
DotName.createSimple("javax.persistence.PreRemove"),
DotName.createSimple("javax.persistence.PreUpdate"));

}
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ void registerBeans(HibernateOrmConfig hibernateOrmConfig,

// Some user-injectable beans are retrieved programmatically and shouldn't be removed
unremovableBeans.produce(UnremovableBeanBuildItem.beanTypes(AttributeConverter.class));
unremovableBeans.produce(UnremovableBeanBuildItem.beanTypes(jpaModel.getPotentialCdiBeanClassNames()));
}

@Consume(InterceptedStaticMethodsTransformersRegisteredBuildItem.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmJoinedSubclassEntityType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmUnionSubclassEntityType;
import org.hibernate.boot.jaxb.mapping.spi.EntityOrMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.spi.JaxbConverter;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddable;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListeners;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings;
import org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaults;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadata;
import org.hibernate.boot.jaxb.mapping.spi.ManagedType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand Down Expand Up @@ -84,9 +90,13 @@ public JpaModelBuildItem discoverModelAndRegisterForReflection() {
enlistJPAModelClasses(collector, ClassNames.JPA_ENTITY);
enlistJPAModelClasses(collector, ClassNames.EMBEDDABLE);
enlistJPAModelClasses(collector, ClassNames.MAPPED_SUPERCLASS);
enlistJPAModelClasses(collector, ClassNames.CONVERTER);
enlistEmbeddedsAndElementCollections(collector);

enlistPotentialCdiBeanClasses(collector, ClassNames.CONVERTER);
for (DotName annotation : HibernateOrmAnnotations.JPA_LISTENER_ANNOTATIONS) {
enlistPotentialCdiBeanClasses(collector, annotation);
}

for (JpaModelPersistenceUnitContributionBuildItem persistenceUnitContribution : persistenceUnitContributions) {
enlistExplicitMappings(collector, persistenceUnitContribution);
}
Expand All @@ -110,6 +120,12 @@ public JpaModelBuildItem discoverModelAndRegisterForReflection() {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, javaType));
}

// 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 @@ -126,8 +142,8 @@ public JpaModelBuildItem discoverModelAndRegisterForReflection() {
}
}

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

private void enlistExplicitMappings(Collector collector,
Expand Down Expand Up @@ -176,21 +192,47 @@ private void enlistOrmXmlMapping(Collector collector, JaxbEntityMappings mapping
String packageName = mapping.getPackage();
String packagePrefix = packageName == null ? "" : packageName + ".";

JaxbPersistenceUnitMetadata metadata = mapping.getPersistenceUnitMetadata();
JaxbPersistenceUnitDefaults defaults = metadata == null ? null : metadata.getPersistenceUnitDefaults();
if (defaults != null) {
enlistOrmXmlMappingListeners(collector, packagePrefix, defaults.getEntityListeners());
}

for (JaxbEntity entity : mapping.getEntity()) {
String name = safeGetClassName(packagePrefix, entity, "entity");
enlistExplicitClass(collector, name);
// The call to 'enlistExplicitClass' above may not
// detect that this class is an entity if it is not annotated
collector.entityTypes.add(name);
enlistOrmXmlMappingManagedClass(collector, packagePrefix, entity, "entity");
}
for (JaxbMappedSuperclass mappedSuperclass : mapping.getMappedSuperclass()) {
String name = safeGetClassName(packagePrefix, mappedSuperclass, "mapped-superclass");
enlistExplicitClass(collector, name);
enlistOrmXmlMappingManagedClass(collector, packagePrefix, mappedSuperclass, "mapped-superclass");
}
for (JaxbEmbeddable embeddable : mapping.getEmbeddable()) {
String name = safeGetClassName(packagePrefix, embeddable, "embeddable");
enlistExplicitClass(collector, name);
}
for (JaxbConverter converter : mapping.getConverter()) {
collector.potentialCdiBeanTypes.add(DotName.createSimple(qualifyIfNecessary(packagePrefix, converter.getClazz())));
}
}

private void enlistOrmXmlMappingManagedClass(Collector collector, String packagePrefix, EntityOrMappedSuperclass managed,
String nodeName) {
String name = safeGetClassName(packagePrefix, managed, nodeName);
enlistExplicitClass(collector, name);
if (managed instanceof JaxbEntity) {
// The call to 'enlistExplicitClass' above may not
// detect that this class is an entity if it is not annotated
collector.entityTypes.add(name);
}

enlistOrmXmlMappingListeners(collector, packagePrefix, managed.getEntityListeners());
}

private void enlistOrmXmlMappingListeners(Collector collector, String packagePrefix, JaxbEntityListeners entityListeners) {
if (entityListeners == null) {
return;
}
for (JaxbEntityListener listener : entityListeners.getEntityListener()) {
collector.potentialCdiBeanTypes.add(DotName.createSimple(qualifyIfNecessary(packagePrefix, listener.getClazz())));
}
}

private static String safeGetClassName(String packagePrefix, ManagedType managedType, String nodeName) {
Expand Down Expand Up @@ -333,6 +375,38 @@ private void enlistJPAModelClasses(Collector collector, DotName dotName) {
}
}

private void enlistPotentialCdiBeanClasses(Collector collector, DotName dotName) {
Collection<AnnotationInstance> jpaAnnotations = index.getAnnotations(dotName);

if (jpaAnnotations == null) {
return;
}

for (AnnotationInstance annotation : jpaAnnotations) {
AnnotationTarget target = annotation.target();
ClassInfo beanType;
switch (target.kind()) {
case CLASS:
beanType = target.asClass();
break;
case FIELD:
beanType = target.asField().declaringClass();
break;
case METHOD:
beanType = target.asMethod().declaringClass();
break;
case METHOD_PARAMETER:
case TYPE:
case RECORD_COMPONENT:
default:
throw new IllegalArgumentException(
"Annotation " + dotName + " was not expected on a target of kind " + target.kind());
}
DotName beanTypeDotName = beanType.name();
collector.potentialCdiBeanTypes.add(beanTypeDotName);
}
}

/**
* Add the class to the reflective list with full method and field access.
* Add the superclasses recursively as well as the interfaces.
Expand Down Expand Up @@ -437,6 +511,7 @@ private static boolean isInJavaPackage(DotName classDotName) {
private static class Collector {
final Set<String> packages = new HashSet<>();
final Set<String> entityTypes = new HashSet<>();
final Set<DotName> potentialCdiBeanTypes = new HashSet<>();
final Set<String> modelTypes = new HashSet<>();
final Set<String> enumTypes = new HashSet<>();
final Set<String> javaTypes = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import javax.persistence.Entity;

import org.jboss.jandex.DotName;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.hibernate.orm.runtime.boot.xml.RecordableXmlMapping;

Expand All @@ -20,13 +22,16 @@ public final class JpaModelBuildItem extends SimpleBuildItem {

private final Set<String> allModelPackageNames = new TreeSet<>();
private final Set<String> entityClassNames = 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<String> allModelClassNames, Map<String, List<RecordableXmlMapping>> xmlMappingsByPU) {
Set<DotName> potentialCdiBeanClassNames, Set<String> allModelClassNames,
Map<String, List<RecordableXmlMapping>> xmlMappingsByPU) {
this.allModelPackageNames.addAll(allModelPackageNames);
this.entityClassNames.addAll(entityClassNames);
this.potentialCdiBeanClassNames.addAll(potentialCdiBeanClassNames);
this.allModelClassNames.addAll(allModelClassNames);
this.xmlMappingsByPU.putAll(xmlMappingsByPU);
}
Expand All @@ -46,7 +51,15 @@ public Set<String> getEntityClassNames() {
}

/**
* @return the list of all model class names: entities, mapped super classes, converters...
* @return the list of classes that might be retrieved by Hibernate ORM as CDI beans,
* e.g. converters, listeners, ...
*/
public Set<DotName> getPotentialCdiBeanClassNames() {
return potentialCdiBeanClassNames;
}

/**
* @return the list of all model class names: entities, mapped super classes, {@link #getPotentialCdiBeanClassNames()}...
*/
public Set<String> getAllModelClassNames() {
return allModelClassNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;

Expand Down Expand Up @@ -55,6 +57,18 @@ public void testNoMissingJpaAnnotation() {
.containsExactlyInAnyOrderElementsOf(jpaMappingAnnotations);
}

@Test
public void testNoMissingJpaListenerAnnotation() {
Set<DotName> jpaMappingAnnotations = findRuntimeAnnotations(jpaIndex);
Pattern listenerAnnotationNamePattern = Pattern.compile(".*\\.(Pre|Post)[^.]+");
jpaMappingAnnotations = jpaMappingAnnotations.stream()
.filter(name -> listenerAnnotationNamePattern.matcher(name.toString()).matches())
.collect(Collectors.toSet());

assertThat(HibernateOrmAnnotations.JPA_LISTENER_ANNOTATIONS)
.containsExactlyInAnyOrderElementsOf(jpaMappingAnnotations);
}

@Test
public void testNoMissingHibernateAnnotation() {
Set<DotName> hibernateMappingAnnotations = findRuntimeAnnotations(hibernateIndex);
Expand Down
Loading