HHH-17460 - Ongoing JPA 32 work

This commit is contained in:
Steve Ebersole 2024-03-13 14:03:11 -05:00
parent b4503cfd90
commit ba26055c56
2 changed files with 42 additions and 3 deletions

View File

@ -13,6 +13,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert; import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate; import org.hibernate.annotations.SQLUpdate;
@ -24,6 +26,7 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbAnyMappingImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainer; import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainer;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainerImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainerImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbBasicImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbBasicImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCachingImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddableImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddableImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedIdImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedImpl;
@ -39,6 +42,7 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistentAttribute;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPluralAttribute; import org.hibernate.boot.jaxb.mapping.spi.JaxbPluralAttribute;
import org.hibernate.boot.jaxb.mapping.spi.JaxbTenantIdImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbTenantIdImpl;
import org.hibernate.boot.models.HibernateAnnotations; import org.hibernate.boot.models.HibernateAnnotations;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.categorize.spi.JpaEventListenerStyle; import org.hibernate.boot.models.categorize.spi.JpaEventListenerStyle;
import org.hibernate.boot.models.xml.internal.attr.BasicAttributeProcessing; import org.hibernate.boot.models.xml.internal.attr.BasicAttributeProcessing;
import org.hibernate.boot.models.xml.internal.attr.BasicIdAttributeProcessing; import org.hibernate.boot.models.xml.internal.attr.BasicIdAttributeProcessing;
@ -66,6 +70,7 @@ import org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies;
import jakarta.persistence.Access; import jakarta.persistence.Access;
import jakarta.persistence.AccessType; import jakarta.persistence.AccessType;
import jakarta.persistence.Cacheable;
import jakarta.persistence.Embeddable; import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId; import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Id; import jakarta.persistence.Id;
@ -463,6 +468,7 @@ public class ManagedTypeProcessor {
XmlAnnotationHelper.applyEntity( jaxbEntity, classDetails, xmlDocumentContext ); XmlAnnotationHelper.applyEntity( jaxbEntity, classDetails, xmlDocumentContext );
XmlAnnotationHelper.applyInheritance( jaxbEntity, classDetails, xmlDocumentContext ); XmlAnnotationHelper.applyInheritance( jaxbEntity, classDetails, xmlDocumentContext );
classDetails.addAnnotationUsage( XmlAnnotationHelper.createAccessAnnotation( classAccessType, classDetails, xmlDocumentContext ) ); classDetails.addAnnotationUsage( XmlAnnotationHelper.createAccessAnnotation( classAccessType, classDetails, xmlDocumentContext ) );
applyCaching( jaxbEntity, classDetails, xmlDocumentContext );
if ( jaxbEntity.isAbstract() != null ) { if ( jaxbEntity.isAbstract() != null ) {
XmlProcessingHelper.makeAnnotation( Abstract.class, classDetails, xmlDocumentContext ); XmlProcessingHelper.makeAnnotation( Abstract.class, classDetails, xmlDocumentContext );
@ -548,6 +554,37 @@ public class ManagedTypeProcessor {
// todo : secondary-tables // todo : secondary-tables
} }
private static void applyCaching(
JaxbEntityImpl jaxbEntity,
MutableClassDetails classDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbEntity.isCacheable() == Boolean.TRUE ) {
final MutableAnnotationUsage<Cacheable> cacheableUsage = JpaAnnotations.CACHEABLE.createUsage(
classDetails,
xmlDocumentContext.getModelBuildingContext()
);
classDetails.addAnnotationUsage( cacheableUsage );
}
final JaxbCachingImpl jaxbCaching = jaxbEntity.getCaching();
if ( jaxbCaching != null ) {
final MutableAnnotationUsage<Cache> cacheableUsage = HibernateAnnotations.CACHE.createUsage(
classDetails,
xmlDocumentContext.getModelBuildingContext()
);
classDetails.addAnnotationUsage( cacheableUsage );
XmlProcessingHelper.setIf( jaxbCaching.getRegion(), "region", cacheableUsage );
XmlProcessingHelper.setIf( convertCacheAccessType( jaxbCaching.getAccess() ), "access", cacheableUsage );
}
}
private static CacheConcurrencyStrategy convertCacheAccessType(org.hibernate.cache.spi.access.AccessType accessType) {
if ( accessType == null ) {
return null;
}
return CacheConcurrencyStrategy.fromAccessType( accessType );
}
private static void applyTenantId( private static void applyTenantId(
MutableClassDetails classDetails, MutableClassDetails classDetails,
JaxbEntityImpl jaxbEntity, JaxbEntityImpl jaxbEntity,

View File

@ -10,6 +10,7 @@ import jakarta.persistence.EntityManager;
import jakarta.persistence.SharedCacheMode; import jakarta.persistence.SharedCacheMode;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
@ -41,9 +42,10 @@ public class XmlTest {
@Test @Test
public void testXmlMappingWithCacheable(EntityManagerFactoryScope scope) { public void testXmlMappingWithCacheable(EntityManagerFactoryScope scope) {
EntityManager em = scope.getEntityManagerFactory().createEntityManager(); EntityPersister entityPersister = scope.getEntityManagerFactory()
SharedSessionContractImplementor session = em.unwrap( SharedSessionContractImplementor.class ); .unwrap( SessionFactoryImplementor.class )
EntityPersister entityPersister= session.getFactory().getMappingMetamodel().getEntityDescriptor( Lighter.class ); .getMappingMetamodel()
.getEntityDescriptor( Lighter.class );
Assertions.assertTrue(entityPersister.canReadFromCache()); Assertions.assertTrue(entityPersister.canReadFromCache());
Assertions.assertTrue(entityPersister.canWriteToCache()); Assertions.assertTrue(entityPersister.canWriteToCache());
} }