HHH-17460 - Ongoing JPA 32 work

This commit is contained in:
Steve Ebersole 2024-04-03 22:18:20 -05:00
parent c4383bcbcc
commit 28d348e20b
44 changed files with 488 additions and 7353 deletions

View File

@ -31,7 +31,6 @@ dependencies {
implementation libs.jandex
implementation libs.classmate
implementation libs.byteBuddy
implementation libs.hcann
implementation jakartaLibs.jaxbApi
implementation jakartaLibs.jaxb

View File

@ -13,8 +13,6 @@ import java.util.HashMap;
import java.util.Map;
import org.hibernate.AssertionFailure;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.archive.scan.internal.StandardScanOptions;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
@ -23,7 +21,6 @@ import org.hibernate.boot.archive.scan.spi.Scanner;
import org.hibernate.boot.archive.spi.ArchiveDescriptorFactory;
import org.hibernate.boot.model.TypeBeanInstanceProducer;
import org.hibernate.boot.model.convert.spi.ConverterDescriptor;
import org.hibernate.boot.model.internal.JPAXMLOverriddenMetadataProvider;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@ -64,7 +61,6 @@ public class BootstrapContextImpl implements BootstrapContext {
private boolean isJpaBootstrap;
private final JavaReflectionManager hcannReflectionManager;
private final ClassmateContext classmateContext;
private ScanOptions scanOptions;
@ -88,7 +84,6 @@ public class BootstrapContextImpl implements BootstrapContext {
this.metadataBuildingOptions = metadataBuildingOptions;
this.classLoaderAccess = new ClassLoaderAccessImpl( serviceRegistry.getService( ClassLoaderService.class ) );
this.hcannReflectionManager = generateHcannReflectionManager();
final StrategySelector strategySelector = serviceRegistry.requireService( StrategySelector.class );
final ConfigurationService configService = serviceRegistry.requireService( ConfigurationService.class );
@ -188,11 +183,6 @@ public class BootstrapContextImpl implements BootstrapContext {
return scannerSetting;
}
@Override
public ReflectionManager getReflectionManager() {
return hcannReflectionManager;
}
@Override
public IndexView getJandexView() {
return jandexView;
@ -338,11 +328,4 @@ public class BootstrapContextImpl implements BootstrapContext {
}
cacheRegionDefinitions.add( cacheRegionDefinition );
}
private JavaReflectionManager generateHcannReflectionManager() {
final JavaReflectionManager reflectionManager = new JavaReflectionManager();
reflectionManager.setMetadataProvider( new JPAXMLOverriddenMetadataProvider( this ) );
return reflectionManager;
}
}

View File

@ -29,7 +29,6 @@ import org.hibernate.SessionFactory;
import org.hibernate.annotations.CollectionTypeRegistration;
import org.hibernate.annotations.Imported;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.SessionFactoryBuilder;
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
@ -1318,7 +1317,7 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector,
private final Map<String,AnnotatedClassType> annotatedClassTypeMap = new HashMap<>();
@Override
public AnnotatedClassType getClassType(XClass clazz) {
public AnnotatedClassType getClassType(ClassDetails clazz) {
AnnotatedClassType type = annotatedClassTypeMap.get( clazz.getName() );
if ( type == null ) {
return addClassType( clazz );
@ -1329,13 +1328,13 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector,
}
@Override
public AnnotatedClassType addClassType(XClass clazz) {
public AnnotatedClassType addClassType(ClassDetails clazz) {
final AnnotatedClassType type = getAnnotatedClassType(clazz);
annotatedClassTypeMap.put( clazz.getName(), type );
return type;
}
private static AnnotatedClassType getAnnotatedClassType(XClass clazz) {
private static AnnotatedClassType getAnnotatedClassType(ClassDetails clazz) {
if ( clazz.isAnnotationPresent( Entity.class ) ) {
if ( clazz.isAnnotationPresent( Embeddable.class ) ) {
throw new AnnotationException( "Invalid class annotated both '@Entity' and '@Embeddable': '" + clazz.getName() + "'" );

View File

@ -11,7 +11,6 @@ import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.boot.model.convert.spi.AutoApplicableConverterDescriptor;
import org.hibernate.boot.model.convert.spi.ConverterDescriptor;
import org.hibernate.boot.spi.MetadataBuildingContext;

View File

@ -79,8 +79,6 @@ public final class AnnotationBinder {
private AnnotationBinder() {}
public static void bindDefaults(MetadataBuildingContext context) {
// todo (jpa32) - remove this. left for now for easy debugging
final Map<?,?> defaults = context.getBootstrapContext().getReflectionManager().getDefaults();
final GlobalRegistrations globalRegistrations = context.getMetadataCollector().getGlobalRegistrations();
// id generators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -75,7 +75,6 @@ import org.hibernate.annotations.SqlFragmentAlias;
import org.hibernate.annotations.Synchronize;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.WhereJoinTable;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.boot.BootLogging;
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
import org.hibernate.boot.model.TypeDefinition;
@ -500,7 +499,6 @@ public abstract class CollectionBinder {
+ "' is annotated both '@OneToMany' and '@ManyToMany'" );
}
final String mappedBy;
final ReflectionManager reflectionManager = context.getBootstrapContext().getReflectionManager();
if ( oneToManyAnn != null ) {
if ( joinColumns.isSecondary() ) {
throw new AnnotationException( "Collection '" + getPath( propertyHolder, inferredData )

View File

@ -1,118 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.model.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Member;
import org.hibernate.Internal;
import org.hibernate.annotations.common.reflection.XAnnotatedElement;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.annotations.common.reflection.java.JavaXMember;
/**
* Manage the various fun-ness of dealing with HCANN...
*
* @author Steve Ebersole
*/
@Internal
public final class HCANNHelper {
public static boolean hasAnnotation(
AnnotatedElement element,
Class<? extends Annotation> annotationToCheck) {
if ( element == null ) {
return false;
}
return element.isAnnotationPresent( annotationToCheck );
}
public static boolean hasAnnotation(
AnnotatedElement element,
Class<? extends Annotation> annotationToCheck,
Class<? extends Annotation> annotationToCheck2) {
if ( element == null ) {
return false;
}
return element.isAnnotationPresent( annotationToCheck )
|| element.isAnnotationPresent( annotationToCheck2 );
}
public static boolean hasAnnotation(
XAnnotatedElement element,
Class<? extends Annotation> annotationToCheck) {
if ( element == null ) {
return false;
}
return element.isAnnotationPresent( annotationToCheck );
}
public static boolean hasAnnotation(
XAnnotatedElement element,
Class<? extends Annotation> annotationToCheck,
Class<? extends Annotation> annotationToCheck2) {
if ( element == null ) {
return false;
}
return element.isAnnotationPresent( annotationToCheck )
|| element.isAnnotationPresent( annotationToCheck2);
}
public static boolean hasAnnotation(
XAnnotatedElement element,
Class<? extends Annotation>... annotationsToCheck) {
assert annotationsToCheck != null && annotationsToCheck.length > 0;
if ( element == null ) {
return false;
}
for ( int i = 0; i < annotationsToCheck.length; i++ ) {
if ( element.isAnnotationPresent( annotationsToCheck[i] ) ) {
return true;
}
}
return false;
}
/**
* @deprecated Prefer using {@link #annotatedElementSignature(JavaXMember)}
*/
@Deprecated
public static String annotatedElementSignature(XProperty property) {
return getUnderlyingMember( property ).toString();
}
public static String annotatedElementSignature(final JavaXMember member) {
return getUnderlyingMember( member ).toString();
}
/**
* @deprecated Prefer using {@link #getUnderlyingMember(JavaXMember)}
*/
@Deprecated
public static Member getUnderlyingMember(XProperty property) {
if ( property instanceof JavaXMember ) {
JavaXMember member = (JavaXMember) property;
return member.getMember();
}
else {
throw new org.hibernate.HibernateException( "Can only extract Member from a XProperty which is a JavaXMember" );
}
}
public static Member getUnderlyingMember(final JavaXMember member) {
return member.getMember();
}
}

View File

@ -1,203 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.model.internal;
import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.annotations.common.reflection.AnnotationReader;
import org.hibernate.annotations.common.reflection.MetadataProvider;
import org.hibernate.annotations.common.reflection.java.JavaMetadataProvider;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGeneratorImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbTableGeneratorImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.ClassLoaderAccess;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.NamedNativeQuery;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.NamedStoredProcedureQuery;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.SqlResultSetMapping;
import jakarta.persistence.TableGenerator;
/**
* MetadataProvider aware of the JPA Deployment descriptor (orm.xml, ...).
*
* @author Emmanuel Bernard
*/
@SuppressWarnings("unchecked")
public class JPAXMLOverriddenMetadataProvider implements MetadataProvider {
private static final MetadataProvider STATELESS_BASE_DELEGATE = new JavaMetadataProvider();
private final ClassLoaderAccess classLoaderAccess;
private final XMLContext xmlContext;
/**
* We allow fully disabling XML sources so to improve the efficiency of
* the boot process for those not using it.
*/
private final boolean xmlMappingEnabled;
private Map<Object, Object> defaults;
private Map<AnnotatedElement, AnnotationReader> cache;
public JPAXMLOverriddenMetadataProvider(BootstrapContext bootstrapContext) {
this( bootstrapContext.getClassLoaderAccess(), bootstrapContext );
}
public JPAXMLOverriddenMetadataProvider(ClassLoaderAccess classLoaderAccess, BootstrapContext bootstrapContext) {
this.classLoaderAccess = classLoaderAccess;
this.xmlContext = new XMLContext( classLoaderAccess, bootstrapContext.getClassmateContext() );
this.xmlMappingEnabled = bootstrapContext.getMetadataBuildingOptions().isXmlMappingEnabled();
}
//all of the above can be safely rebuilt from XMLContext: only XMLContext this object is serialized
@Override
public AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement) {
if ( cache == null ) {
cache = new HashMap<>(50 );
}
AnnotationReader reader = cache.get( annotatedElement );
if (reader == null) {
if ( xmlContext.hasContext() ) {
reader = new JPAXMLOverriddenAnnotationReader( annotatedElement, xmlContext, classLoaderAccess );
}
else {
reader = STATELESS_BASE_DELEGATE.getAnnotationReader( annotatedElement );
}
cache.put( annotatedElement, reader );
}
return reader;
}
// @Override
// FIXME this method was introduced in HCANN 5.1.1: we can't mark it as @Override yet,
// but it's expected to be invoked when we're running with the right HCANN version.
public void reset() {
//It's better to remove the HashMap, as it could grow rather large:
//when doing a clear() the internal buckets array is not scaled down.
this.cache = null;
}
@Override
public Map<Object, Object> getDefaults() {
if ( !xmlMappingEnabled ) {
return Collections.emptyMap();
}
else {
if ( defaults == null ) {
defaults = new HashMap<>();
XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema();
defaults.put( "schema", xmlDefaults.getSchema() );
defaults.put( "catalog", xmlDefaults.getCatalog() );
defaults.put( "delimited-identifier", xmlDefaults.getDelimitedIdentifier() );
defaults.put( "cascade-persist", xmlDefaults.getCascadePersist() );
List<Class> entityListeners = new ArrayList<Class>();
for ( String className : xmlContext.getDefaultEntityListeners() ) {
try {
entityListeners.add( classLoaderAccess.classForName( className ) );
}
catch (ClassLoadingException e) {
throw new IllegalStateException( "Default entity listener class not found: " + className );
}
}
defaults.put( EntityListeners.class, entityListeners );
for ( JaxbEntityMappingsImpl entityMappings : xmlContext.getAllDocuments() ) {
List<JaxbSequenceGeneratorImpl> jaxbSequenceGenerators = entityMappings.getSequenceGenerators();
List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get( SequenceGenerator.class );
if ( sequenceGenerators == null ) {
sequenceGenerators = new ArrayList<>();
defaults.put( SequenceGenerator.class, sequenceGenerators );
}
for ( JaxbSequenceGeneratorImpl element : jaxbSequenceGenerators ) {
sequenceGenerators.add( JPAXMLOverriddenAnnotationReader.buildSequenceGeneratorAnnotation( element ) );
}
List<JaxbTableGeneratorImpl> jaxbTableGenerators = entityMappings.getTableGenerators();
List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get( TableGenerator.class );
if ( tableGenerators == null ) {
tableGenerators = new ArrayList<>();
defaults.put( TableGenerator.class, tableGenerators );
}
for ( JaxbTableGeneratorImpl element : jaxbTableGenerators ) {
tableGenerators.add(
JPAXMLOverriddenAnnotationReader.buildTableGeneratorAnnotation(
element,
xmlDefaults
)
);
}
List<NamedQuery> namedQueries = ( List<NamedQuery> ) defaults.get( NamedQuery.class );
if ( namedQueries == null ) {
namedQueries = new ArrayList<>();
defaults.put( NamedQuery.class, namedQueries );
}
List<NamedQuery> currentNamedQueries = JPAXMLOverriddenAnnotationReader.buildNamedQueries(
entityMappings.getNamedQueries(),
xmlDefaults,
classLoaderAccess
);
namedQueries.addAll( currentNamedQueries );
List<NamedNativeQuery> namedNativeQueries = ( List<NamedNativeQuery> ) defaults.get( NamedNativeQuery.class );
if ( namedNativeQueries == null ) {
namedNativeQueries = new ArrayList<>();
defaults.put( NamedNativeQuery.class, namedNativeQueries );
}
List<NamedNativeQuery> currentNamedNativeQueries = JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(
entityMappings.getNamedNativeQueries(),
xmlDefaults,
classLoaderAccess
);
namedNativeQueries.addAll( currentNamedNativeQueries );
List<SqlResultSetMapping> sqlResultSetMappings = ( List<SqlResultSetMapping> ) defaults.get(
SqlResultSetMapping.class
);
if ( sqlResultSetMappings == null ) {
sqlResultSetMappings = new ArrayList<>();
defaults.put( SqlResultSetMapping.class, sqlResultSetMappings );
}
List<SqlResultSetMapping> currentSqlResultSetMappings = JPAXMLOverriddenAnnotationReader.buildSqlResultSetMappings(
entityMappings.getSqlResultSetMappings(),
xmlDefaults,
classLoaderAccess
);
sqlResultSetMappings.addAll( currentSqlResultSetMappings );
List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>)defaults.get( NamedStoredProcedureQuery.class );
if(namedStoredProcedureQueries==null){
namedStoredProcedureQueries = new ArrayList<>( );
defaults.put( NamedStoredProcedureQuery.class, namedStoredProcedureQueries );
}
List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAXMLOverriddenAnnotationReader
.buildNamedStoreProcedureQueries(
entityMappings.getNamedProcedureQueries(),
xmlDefaults,
classLoaderAccess
);
namedStoredProcedureQueries.addAll( currentNamedStoredProcedureQueries );
}
}
return defaults;
}
}
public XMLContext getXMLContext() {
return xmlContext;
}
}

View File

@ -1,30 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.model.internal;
import org.hibernate.annotations.common.reflection.Filter;
/**
* @author Emmanuel Bernard
* @author Steve Ebersole
*/
public class PersistentAttributeFilter implements Filter {
/**
* Singleton access
*/
public static final PersistentAttributeFilter INSTANCE = new PersistentAttributeFilter();
@Override
public boolean returnStatic() {
return false;
}
@Override
public boolean returnTransient() {
return false;
}
}

View File

@ -1,224 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.model.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainer;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainerImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbBasicImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollectionImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbLifecycleCallback;
import org.hibernate.boot.jaxb.mapping.spi.JaxbLifecycleCallbackContainer;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManagedType;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManyToManyImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOneImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOneToManyImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOneImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistentAttribute;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPostLoadImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPostPersistImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPostRemoveImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPostUpdateImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPrePersistImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPreRemoveImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPreUpdateImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbTenantIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbTransientImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbVersionImpl;
/**
* Reproduces what we used to do with a {@code List<Element>} in {@link JPAXMLOverriddenAnnotationReader},
* with the following constraints:
* <ul>
* <li>Preserve type safety</li>
* <li>Only create lists if we actually have elements (most lists should be empty in most cases)</li>
* </ul>
*/
public final class PropertyMappingElementCollector {
public static final Function<JaxbPersistentAttribute, String> PERSISTENT_ATTRIBUTE_NAME = JaxbPersistentAttribute::getName;
public static final Function<JaxbTransientImpl, String> JAXB_TRANSIENT_NAME = JaxbTransientImpl::getName;
static final Function<JaxbLifecycleCallback, String> LIFECYCLE_CALLBACK_NAME = JaxbLifecycleCallback::getMethodName;
private final String propertyName;
private List<JaxbIdImpl> id;
private List<JaxbEmbeddedIdImpl> embeddedId;
private List<JaxbVersionImpl> version;
private List<JaxbBasicImpl> basic;
private List<JaxbEmbeddedImpl> embedded;
private List<JaxbOneToOneImpl> oneToOne;
private List<JaxbManyToOneImpl> manyToOne;
private List<JaxbElementCollectionImpl> elementCollection;
private List<JaxbOneToManyImpl> oneToMany;
private List<JaxbManyToManyImpl> manyToMany;
private List<JaxbTransientImpl> _transient;
private List<JaxbPrePersistImpl> prePersist;
private List<JaxbPostPersistImpl> postPersist;
private List<JaxbPreRemoveImpl> preRemove;
private List<JaxbPostRemoveImpl> postRemove;
private List<JaxbPreUpdateImpl> preUpdate;
private List<JaxbPostUpdateImpl> postUpdate;
private List<JaxbPostLoadImpl> postLoad;
public PropertyMappingElementCollector(String propertyName) {
this.propertyName = propertyName;
}
public boolean isEmpty() {
return allNullOrEmpty( id, embeddedId, basic, version, manyToOne, oneToMany, oneToOne, manyToMany,
elementCollection, embedded, _transient,
prePersist, postPersist, preRemove, postRemove, preUpdate, postUpdate, postLoad );
}
private boolean allNullOrEmpty(List<?>... lists) {
for ( List<?> list : lists ) {
if ( list != null && !list.isEmpty() ) {
return false;
}
}
return true;
}
private <T> List<T> defaultToEmpty(List<T> list) {
return list == null ? Collections.emptyList() : list;
}
public void collectPersistentAttributesIfMatching(JaxbAttributesContainer container) {
if ( container instanceof JaxbAttributesContainerImpl jaxbAttributes ) {
id = collectIfMatching( id, jaxbAttributes.getIdAttributes(), PERSISTENT_ATTRIBUTE_NAME );
embeddedId = collectIfMatching( embeddedId, jaxbAttributes.getEmbeddedIdAttribute(), PERSISTENT_ATTRIBUTE_NAME );
version = collectIfMatching( version, jaxbAttributes.getVersion(), PERSISTENT_ATTRIBUTE_NAME );
}
basic = collectIfMatching( basic, container.getBasicAttributes(), PERSISTENT_ATTRIBUTE_NAME );
manyToOne = collectIfMatching( manyToOne, container.getManyToOneAttributes(), PERSISTENT_ATTRIBUTE_NAME );
oneToMany = collectIfMatching( oneToMany, container.getOneToManyAttributes(), PERSISTENT_ATTRIBUTE_NAME );
oneToOne = collectIfMatching( oneToOne, container.getOneToOneAttributes(), PERSISTENT_ATTRIBUTE_NAME );
manyToMany = collectIfMatching( manyToMany, container.getManyToManyAttributes(), PERSISTENT_ATTRIBUTE_NAME );
elementCollection = collectIfMatching( elementCollection, container.getElementCollectionAttributes(), PERSISTENT_ATTRIBUTE_NAME );
embedded = collectIfMatching( embedded, container.getEmbeddedAttributes(), PERSISTENT_ATTRIBUTE_NAME );
_transient = collectIfMatching( _transient, container.getTransients(), JAXB_TRANSIENT_NAME );
}
public void collectLifecycleCallbacksIfMatching(JaxbLifecycleCallbackContainer container) {
prePersist = collectIfMatching( prePersist, container.getPrePersist(), LIFECYCLE_CALLBACK_NAME );
postPersist = collectIfMatching( postPersist, container.getPostPersist(), LIFECYCLE_CALLBACK_NAME );
preRemove = collectIfMatching( preRemove, container.getPreRemove(), LIFECYCLE_CALLBACK_NAME );
postRemove = collectIfMatching( postRemove, container.getPostRemove(), LIFECYCLE_CALLBACK_NAME );
preUpdate = collectIfMatching( preUpdate, container.getPreUpdate(), LIFECYCLE_CALLBACK_NAME );
postUpdate = collectIfMatching( postUpdate, container.getPostUpdate(), LIFECYCLE_CALLBACK_NAME );
postLoad = collectIfMatching( postLoad, container.getPostLoad(), LIFECYCLE_CALLBACK_NAME );
}
public void collectTenantIdIfMatching(JaxbManagedType managedType) {
if ( managedType instanceof JaxbEntityImpl ) {
JaxbTenantIdImpl tenantId = ( (JaxbEntityImpl) managedType ).getTenantId();
basic = collectIfMatching( basic, tenantId, PERSISTENT_ATTRIBUTE_NAME );
}
}
private <T> List<T> collectIfMatching(List<T> collected, List<T> candidates,
Function<? super T, String> nameGetter) {
List<T> result = collected;
for ( T candidate : candidates ) {
result = collectIfMatching( result, candidate, nameGetter );
}
return result;
}
private <T> List<T> collectIfMatching(List<T> collected, T candidate, Function<? super T, String> nameGetter) {
List<T> result = collected;
if ( candidate != null && propertyName.equals( nameGetter.apply( candidate ) ) ) {
if ( result == null ) {
result = new ArrayList<>();
}
result.add( candidate );
}
return result;
}
public List<JaxbIdImpl> getId() {
return defaultToEmpty( id );
}
public List<JaxbEmbeddedIdImpl> getEmbeddedId() {
return defaultToEmpty( embeddedId );
}
public List<JaxbBasicImpl> getBasic() {
return defaultToEmpty( basic );
}
public List<JaxbVersionImpl> getVersion() {
return defaultToEmpty( version );
}
public List<JaxbManyToOneImpl> getManyToOne() {
return defaultToEmpty( manyToOne );
}
public List<JaxbOneToManyImpl> getOneToMany() {
return defaultToEmpty( oneToMany );
}
public List<JaxbOneToOneImpl> getOneToOne() {
return defaultToEmpty( oneToOne );
}
public List<JaxbManyToManyImpl> getManyToMany() {
return defaultToEmpty( manyToMany );
}
public List<JaxbElementCollectionImpl> getElementCollection() {
return defaultToEmpty( elementCollection );
}
public List<JaxbEmbeddedImpl> getEmbedded() {
return defaultToEmpty( embedded );
}
public List<JaxbTransientImpl> getTransient() {
return defaultToEmpty( _transient );
}
public List<JaxbPrePersistImpl> getPrePersist() {
return defaultToEmpty( prePersist );
}
public List<JaxbPostPersistImpl> getPostPersist() {
return defaultToEmpty( postPersist );
}
public List<JaxbPreRemoveImpl> getPreRemove() {
return defaultToEmpty( preRemove );
}
public List<JaxbPostRemoveImpl> getPostRemove() {
return defaultToEmpty( postRemove );
}
public List<JaxbPreUpdateImpl> getPreUpdate() {
return defaultToEmpty( preUpdate );
}
public List<JaxbPostUpdateImpl> getPostUpdate() {
return defaultToEmpty( postUpdate );
}
public List<JaxbPostLoadImpl> getPostLoad() {
return defaultToEmpty( postLoad );
}
}

View File

@ -33,6 +33,7 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
private final boolean entitiesImplicitlyLazy;
private final boolean pluralAttributesImplicitlyLazy;
private final AccessType implicitCacheAccessType;
private final EnumSet<CascadeType> cascadeTypes;
private final CollectionClassification implicitListClassification;
public OverriddenMappingDefaults(
@ -44,7 +45,7 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
String implicitDiscriminatorColumnName,
String implicitPackageName,
boolean autoImportEnabled,
String implicitCascadeStyleName,
EnumSet<CascadeType> cascadeTypes,
jakarta.persistence.AccessType implicitPropertyAccessType,
String implicitPropertyAccessorName,
boolean entitiesImplicitlyLazy,
@ -59,6 +60,7 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
this.implicitDiscriminatorColumnName = implicitDiscriminatorColumnName;
this.implicitPackageName = implicitPackageName;
this.autoImportEnabled = autoImportEnabled;
this.cascadeTypes = cascadeTypes;
this.implicitPropertyAccessType = implicitPropertyAccessType;
this.implicitPropertyAccessorName = implicitPropertyAccessorName;
this.entitiesImplicitlyLazy = entitiesImplicitlyLazy;
@ -109,7 +111,7 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
@Override
public EnumSet<CascadeType> getDefaultCascadeTypes() {
return EnumSet.noneOf( CascadeType.class );
return cascadeTypes;
}
@Override
@ -151,7 +153,7 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
private String implicitDiscriminatorColumnName;
private String implicitPackageName;
private boolean autoImportEnabled;
private String implicitCascadeStyleName;
private EnumSet<CascadeType> implicitCascadeTypes;
private jakarta.persistence.AccessType implicitPropertyAccessType;
private String implicitPropertyAccessorName;
private boolean entitiesImplicitlyLazy;
@ -168,7 +170,8 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
this.implicitDiscriminatorColumnName = parentDefaults.getDefaultDiscriminatorColumnName();
this.implicitPackageName = parentDefaults.getDefaultPackageName();
this.autoImportEnabled = parentDefaults.isDefaultAutoImport();
this.implicitCascadeStyleName = "none";
this.implicitCascadeTypes = parentDefaults.getDefaultCascadeTypes();
this.implicitPropertyAccessorName = parentDefaults.getDefaultAccessStrategyName();
this.entitiesImplicitlyLazy = parentDefaults.isDefaultEntityLaziness();
this.pluralAttributesImplicitlyLazy = parentDefaults.isDefaultCollectionLaziness();
@ -227,12 +230,12 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
return this;
}
public Builder setImplicitCascadeStyleName(String implicitCascadeStyleName) {
if ( StringHelper.isNotEmpty( implicitCascadeStyleName ) ) {
this.implicitCascadeStyleName = implicitCascadeStyleName;
}
return this;
}
// public Builder setImplicitCascadeStyleName(String implicitCascadeStyleName) {
// if ( StringHelper.isNotEmpty( implicitCascadeStyleName ) ) {
// this.implicitCascadeStyleName = implicitCascadeStyleName;
// }
// return this;
// }
public Builder setImplicitPropertyAccessType(jakarta.persistence.AccessType accessType) {
if ( accessType != null ) {
@ -286,7 +289,7 @@ public class OverriddenMappingDefaults implements EffectiveMappingDefaults {
implicitDiscriminatorColumnName,
implicitPackageName,
autoImportEnabled,
implicitCascadeStyleName,
implicitCascadeTypes,
implicitPropertyAccessType,
implicitPropertyAccessorName,
entitiesImplicitlyLazy,

View File

@ -72,7 +72,7 @@ public class MappingDocument implements HbmLocalMetadataBuildingContext, Metadat
.setImplicitCatalogName( documentRoot.getCatalog() )
.setImplicitPackageName( documentRoot.getPackage() )
.setImplicitPropertyAccessorName( documentRoot.getDefaultAccess() )
.setImplicitCascadeStyleName( documentRoot.getDefaultCascade() )
// .setImplicitCascadeStyleName( documentRoot.getDefaultCascade() )
.setEntitiesImplicitlyLazy( documentRoot.isDefaultLazy() )
.setAutoImportEnabled( documentRoot.isAutoImport() )
.setPluralAttributesImplicitlyLazy( documentRoot.isDefaultLazy() )

View File

@ -8,34 +8,26 @@ package org.hibernate.boot.models.internal;
import java.lang.annotation.Annotation;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AnnotationTarget;
import org.hibernate.models.spi.AnnotationUsage;
import org.hibernate.models.spi.MutableAnnotationUsage;
import org.hibernate.models.spi.MutableClassDetails;
import org.hibernate.models.spi.SourceModelBuildingContext;
import jakarta.persistence.NamedQueries;
public class AnnotationUsageHelper {
public static <A extends Annotation> void applyAttributeIfSpecified(
String attributeName,
Object value,
MutableAnnotationUsage<A> annotationUsage) {
if ( value != null ) {
annotationUsage.setAttributeValue( attributeName, value );
}
XmlAnnotationHelper.applyOptionalAttribute( annotationUsage, attributeName, value );
}
public static <A extends Annotation> void applyStringAttributeIfSpecified(
String attributeName,
String value,
MutableAnnotationUsage<A> annotationUsage) {
if ( StringHelper.isNotEmpty( value ) ) {
annotationUsage.setAttributeValue( attributeName, value );
}
XmlAnnotationHelper.applyOptionalAttribute( annotationUsage, attributeName, value );
}
public static <A extends Annotation> MutableAnnotationUsage<A> getOrCreateUsage(

View File

@ -11,7 +11,6 @@ import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
@ -60,22 +59,17 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbDiscriminatorFormulaImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddedIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbForeignKeyImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbGeneratedValueImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbHbmFilterImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdClassImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbIndexImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJoinColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJoinTableImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbLifecycleCallback;
import org.hibernate.boot.jaxb.mapping.spi.JaxbLifecycleCallbackContainer;
import org.hibernate.boot.jaxb.mapping.spi.JaxbLobImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyJoinColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbNamedAttributeNodeImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbNamedEntityGraphImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbNamedSubgraphImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbNationalizedImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbNaturalId;
import org.hibernate.boot.jaxb.mapping.spi.JaxbSchemaAware;
@ -86,12 +80,15 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbUniqueConstraintImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbUserTypeImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbUuidGeneratorImpl;
import org.hibernate.boot.jaxb.mapping.spi.db.JaxbCheckable;
import org.hibernate.boot.jaxb.mapping.spi.db.JaxbColumnJoined;
import org.hibernate.boot.jaxb.mapping.spi.db.JaxbTableMapping;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.categorize.spi.JpaEventListener;
import org.hibernate.boot.models.categorize.spi.JpaEventListenerStyle;
import org.hibernate.boot.models.internal.AnnotationUsageHelper;
import org.hibernate.boot.models.xml.internal.db.ColumnProcessing;
import org.hibernate.boot.models.xml.internal.db.ForeignKeyProcessing;
import org.hibernate.boot.models.xml.internal.db.JoinColumnProcessing;
import org.hibernate.boot.models.xml.internal.db.TableProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocument;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
@ -126,21 +123,15 @@ import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Index;
import jakarta.persistence.Inheritance;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.Lob;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.MapKeyJoinColumn;
import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.NamedSubgraph;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;
import jakarta.persistence.PostRemove;
@ -157,7 +148,6 @@ import jakarta.persistence.UniqueConstraint;
import static java.lang.Boolean.FALSE;
import static java.util.Collections.emptyList;
import static org.hibernate.boot.models.JpaAnnotations.NAMED_ATTRIBUTE_NODE;
import static org.hibernate.boot.models.xml.internal.XmlProcessingHelper.makeNestedAnnotation;
/**
@ -167,6 +157,52 @@ import static org.hibernate.boot.models.xml.internal.XmlProcessingHelper.makeNes
*/
public class XmlAnnotationHelper {
public static void applyOptionalAttribute(MutableAnnotationUsage<? extends Annotation> annotationUsage, String attributeName, Object value) {
if ( value != null ) {
annotationUsage.setAttributeValue( attributeName, value );
}
}
public static void applyOptionalAttribute(MutableAnnotationUsage<? extends Annotation> annotationUsage, String attributeName, String value) {
if ( StringHelper.isNotEmpty( value ) ) {
annotationUsage.setAttributeValue( attributeName, value );
}
}
public static <S> void applyOptionalAttribute(
MutableAnnotationUsage<? extends Annotation> annotationUsage,
String attributeName,
S valueSource,
Function<S,Object> valueExtractor) {
if ( valueSource == null ) {
return;
}
final Object value = valueExtractor.apply( valueSource );
if ( value == null ) {
return;
}
annotationUsage.setAttributeValue( attributeName, value );
}
public static <S> void applyOptionalStringAttribute(
MutableAnnotationUsage<? extends Annotation> annotationUsage,
String attributeName,
S valueSource,
Function<S,String> valueExtractor) {
if ( valueSource == null ) {
return;
}
final String value = valueExtractor.apply( valueSource );
if ( StringHelper.isEmpty( value ) ) {
return;
}
annotationUsage.setAttributeValue( attributeName, value );
}
/**
* Handle creating {@linkplain Entity @Entity} from an {@code <entity/>} element.
* Used in both complete and override modes.
@ -217,127 +253,7 @@ public class XmlAnnotationHelper {
return null;
}
return createJoinColumnAnnotation( jaxbJoinColumn, memberDetails, JoinColumn.class, xmlDocumentContext );
}
public static MutableAnnotationUsage<JoinColumn> applyJoinColumn(
JaxbJoinColumnImpl jaxbJoinColumn,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbJoinColumn == null ) {
return null;
}
return createJoinColumnAnnotation( jaxbJoinColumn, JoinColumn.class, xmlDocumentContext );
}
public static <A extends Annotation> MutableAnnotationUsage<A> createJoinColumnAnnotation(
JaxbColumnJoined jaxbJoinColumn,
MutableMemberDetails memberDetails,
Class<A> annotationType,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<A> joinColumnAnn = XmlProcessingHelper.getOrMakeNamedAnnotation(
annotationType,
jaxbJoinColumn.getName(),
memberDetails,
xmlDocumentContext
);
final AnnotationDescriptor<A> joinColumnDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( annotationType );
ColumnProcessing.applyColumnDetails( jaxbJoinColumn, memberDetails, joinColumnAnn, xmlDocumentContext );
applyOr( jaxbJoinColumn, JaxbColumnJoined::getReferencedColumnName, "referencedColumnName", joinColumnAnn, joinColumnDescriptor );
final JaxbForeignKeyImpl jaxbForeignKey = jaxbJoinColumn.getForeignKey();
if ( jaxbForeignKey != null ) {
joinColumnAnn.setAttributeValue(
"foreignKey",
createForeignKeyAnnotation( jaxbForeignKey, memberDetails, xmlDocumentContext )
);
}
return joinColumnAnn;
}
public static <A extends Annotation> MutableAnnotationUsage<A> createJoinColumnAnnotation(
JaxbColumnJoined jaxbJoinColumn,
Class<A> annotationType,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<A> joinColumnAnn = XmlProcessingHelper.getOrMakeAnnotation( annotationType, xmlDocumentContext );
final AnnotationDescriptor<A> joinColumnDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( annotationType );
ColumnProcessing.applyColumnDetails( jaxbJoinColumn, joinColumnAnn, xmlDocumentContext );
applyOr( jaxbJoinColumn, JaxbColumnJoined::getReferencedColumnName, "referencedColumnName", joinColumnAnn, joinColumnDescriptor );
final JaxbForeignKeyImpl jaxbForeignKey = jaxbJoinColumn.getForeignKey();
if ( jaxbForeignKey != null ) {
joinColumnAnn.setAttributeValue(
"foreignKey",
createForeignKeyAnnotation( jaxbForeignKey, xmlDocumentContext )
);
}
return joinColumnAnn;
}
public static void applyJoinColumns(
List<JaxbJoinColumnImpl> jaxbJoinColumns,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return;
}
if ( jaxbJoinColumns.size() == 1 ) {
XmlAnnotationHelper.applyJoinColumn( jaxbJoinColumns.get( 0 ), memberDetails, xmlDocumentContext );
}
else {
final MutableAnnotationUsage<JoinColumns> columnsAnn = XmlProcessingHelper.makeAnnotation(
JoinColumns.class,
memberDetails,
xmlDocumentContext
);
columnsAnn.setAttributeValue( "value", createJoinColumns( jaxbJoinColumns, memberDetails, xmlDocumentContext ) );
}
}
public static List<AnnotationUsage<JoinColumn>> createJoinColumns(
List<JaxbJoinColumnImpl> jaxbJoinColumns,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return Collections.emptyList();
}
final List<AnnotationUsage<JoinColumn>> joinColumns = new ArrayList<>( jaxbJoinColumns.size() );
jaxbJoinColumns.forEach( jaxbJoinColumn -> {
final MutableAnnotationUsage<JoinColumn> annotationUsage = createJoinColumnAnnotation(
jaxbJoinColumn,
memberDetails,
JoinColumn.class,
xmlDocumentContext
);
joinColumns.add( annotationUsage );
} );
return joinColumns;
}
public static List<AnnotationUsage<JoinColumn>> createJoinColumns(
List<JaxbJoinColumnImpl> jaxbJoinColumns,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return Collections.emptyList();
}
final List<AnnotationUsage<JoinColumn>> joinColumns = new ArrayList<>( jaxbJoinColumns.size() );
jaxbJoinColumns.forEach( jaxbJoinColumn -> {
joinColumns.add( applyJoinColumn( jaxbJoinColumn, xmlDocumentContext ) );
} );
return joinColumns;
return JoinColumnProcessing.createJoinColumnAnnotation( jaxbJoinColumn, memberDetails, JoinColumn.class, xmlDocumentContext );
}
public static <T,N> void applyOr(
@ -460,57 +376,6 @@ public class XmlAnnotationHelper {
applyOr( jaxbCollectionId, JaxbCollectionIdImpl::getGenerator, "generator", collectionIdAnn, collectionIdDescriptor );
}
public static void applyForeignKey(
JaxbForeignKeyImpl jaxbForeignKey,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbForeignKey == null ) {
return;
}
createForeignKeyAnnotation( jaxbForeignKey, memberDetails, xmlDocumentContext );
}
public static MutableAnnotationUsage<ForeignKey> createForeignKeyAnnotation(
JaxbForeignKeyImpl jaxbForeignKey,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<ForeignKey> foreignKeyAnn = XmlProcessingHelper.getOrMakeNamedAnnotation(
ForeignKey.class,
jaxbForeignKey.getName(),
memberDetails,
xmlDocumentContext
);
final AnnotationDescriptor<ForeignKey> foreignKeyDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( ForeignKey.class );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getName, "name", foreignKeyAnn, foreignKeyDescriptor );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getConstraintMode, "value", foreignKeyAnn, foreignKeyDescriptor );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getForeignKeyDefinition, "foreignKeyDefinition", foreignKeyAnn, foreignKeyDescriptor );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getOptions, "options", foreignKeyAnn, foreignKeyDescriptor );
return foreignKeyAnn;
}
public static MutableAnnotationUsage<ForeignKey> createForeignKeyAnnotation(
JaxbForeignKeyImpl jaxbForeignKey,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<ForeignKey> foreignKeyAnn = XmlProcessingHelper.getOrMakeAnnotation( ForeignKey.class, xmlDocumentContext );
final AnnotationDescriptor<ForeignKey> foreignKeyDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( ForeignKey.class );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getName, "name", foreignKeyAnn, foreignKeyDescriptor );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getConstraintMode, "value", foreignKeyAnn, foreignKeyDescriptor );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getForeignKeyDefinition, "foreignKeyDefinition", foreignKeyAnn, foreignKeyDescriptor );
applyOr( jaxbForeignKey, JaxbForeignKeyImpl::getOptions, "options", foreignKeyAnn, foreignKeyDescriptor );
return foreignKeyAnn;
}
public static void applyMapKeyColumn(
JaxbMapKeyColumnImpl jaxbMapKeyColumn,
MutableMemberDetails memberDetails,
@ -532,7 +397,7 @@ public class XmlAnnotationHelper {
return;
}
createJoinColumnAnnotation( jaxbMapKeyJoinColumn, memberDetails, MapKeyJoinColumn.class, xmlDocumentContext );
JoinColumnProcessing.createJoinColumnAnnotation( jaxbMapKeyJoinColumn, memberDetails, MapKeyJoinColumn.class, xmlDocumentContext );
}
public static void applyCascading(
@ -545,7 +410,7 @@ public class XmlAnnotationHelper {
// We always use Hibernate specific `org.hibernate.annotations.CascadeType`
// since it is a superset of `jakarta.persistence.CascadeType`
final List<CascadeType> cascadeTypes = new ArrayList<>();
final List<CascadeType> cascadeTypes = new ArrayList<>( xmlDocumentContext.getEffectiveDefaults().getDefaultCascadeTypes() );
if ( jaxbCascadeType.getCascadeAll() != null ) {
cascadeTypes.add( CascadeType.ALL );
}
@ -587,23 +452,19 @@ public class XmlAnnotationHelper {
return;
}
final List<AnnotationUsage<UniqueConstraint>> uniqueConstraints = new ArrayList<>( jaxbUniqueConstraints.size() );
jaxbUniqueConstraints.forEach( jaxbUniqueConstraint -> {
final MutableAnnotationUsage<UniqueConstraint> uniqueConstraintAnn = XmlProcessingHelper.getOrMakeAnnotation(
UniqueConstraint.class,
target,
xmlDocumentContext
);
final AnnotationDescriptor<UniqueConstraint> uniqueConstraintDescriptor = xmlDocumentContext.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( UniqueConstraint.class );
XmlProcessingHelper.applyAttributeIfSpecified( "name", jaxbUniqueConstraint.getName(), uniqueConstraintAnn );
XmlProcessingHelper.applyAttributeIfSpecified( "options", jaxbUniqueConstraint.getOptions(), uniqueConstraintAnn );
uniqueConstraintAnn.setAttributeValue( "columnNames", jaxbUniqueConstraint.getColumnName() );
uniqueConstraints.add( uniqueConstraintAnn );
} );
final List<AnnotationUsage<UniqueConstraint>> uniqueConstraintUsages = new ArrayList<>( jaxbUniqueConstraints.size() );
annotationUsage.setAttributeValue( "uniqueConstraints", uniqueConstraintUsages );
annotationUsage.setAttributeValue( "uniqueConstraints", uniqueConstraints );
jaxbUniqueConstraints.forEach( (jaxbUniqueConstraint) -> {
final MutableAnnotationUsage<UniqueConstraint> ucUsage = JpaAnnotations.UNIQUE_CONSTRAINT.createUsage(
target,
xmlDocumentContext.getModelBuildingContext()
);
XmlAnnotationHelper.applyOptionalAttribute( ucUsage, "name", jaxbUniqueConstraint.getName() );
XmlAnnotationHelper.applyOptionalAttribute( ucUsage, "options", jaxbUniqueConstraint.getOptions() );
ucUsage.setAttributeValue( "columnNames", jaxbUniqueConstraint.getColumnName() );
uniqueConstraintUsages.add( ucUsage );
} );
}
public static <A extends Annotation> void applyIndexes(
@ -633,52 +494,6 @@ public class XmlAnnotationHelper {
annotationUsage.setAttributeValue( "indexes", indexes );
}
public static MutableAnnotationUsage<JoinTable> applyJoinTable(
JaxbJoinTableImpl jaxbJoinTable,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbJoinTable == null ) {
return null;
}
final MutableAnnotationUsage<JoinTable> joinTableAnn = XmlProcessingHelper.getOrMakeAnnotation(
JoinTable.class,
memberDetails,
xmlDocumentContext
);
final AnnotationDescriptor<JoinTable> joinTableDescriptor = xmlDocumentContext.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( JoinTable.class );
applyOr( jaxbJoinTable, JaxbJoinTableImpl::getName, "name", joinTableAnn, joinTableDescriptor );
applyTableAttributes( jaxbJoinTable, memberDetails, joinTableAnn, joinTableDescriptor, xmlDocumentContext );
final List<JaxbJoinColumnImpl> joinColumns = jaxbJoinTable.getJoinColumn();
if ( CollectionHelper.isNotEmpty( joinColumns ) ) {
joinTableAnn.setAttributeValue( "joinColumns", createJoinColumns( joinColumns, xmlDocumentContext ) );
}
final List<JaxbJoinColumnImpl> inverseJoinColumns = jaxbJoinTable.getInverseJoinColumn();
if ( CollectionHelper.isNotEmpty( inverseJoinColumns ) ) {
joinTableAnn.setAttributeValue( "inverseJoinColumns", createJoinColumns( inverseJoinColumns, xmlDocumentContext ) );
}
if ( jaxbJoinTable.getForeignKey() != null ) {
joinTableAnn.setAttributeValue(
"foreignKey",
createForeignKeyAnnotation( jaxbJoinTable.getForeignKey(), xmlDocumentContext )
);
}
if ( jaxbJoinTable.getInverseForeignKey() != null ) {
joinTableAnn.setAttributeValue(
"inverseForeignKey",
createForeignKeyAnnotation( jaxbJoinTable.getInverseForeignKey(), memberDetails, xmlDocumentContext )
);
}
return joinTableAnn;
}
public static <A extends Annotation> void applyCheckConstraints(
JaxbCheckable jaxbCheckable,
MutableAnnotationTarget target,
@ -912,18 +727,18 @@ public class XmlAnnotationHelper {
XmlProcessingHelper.applyAttributeIfSpecified( "name", jaxbOverride.getName(), annotationUsage );
final List<JaxbJoinColumnImpl> joinColumns = jaxbOverride.getJoinColumns();
if ( CollectionHelper.isNotEmpty( joinColumns ) ) {
annotationUsage.setAttributeValue( "joinColumns", createJoinColumns( joinColumns, memberDetails, xmlDocumentContext ) );
annotationUsage.setAttributeValue( "joinColumns", JoinColumnProcessing.createJoinColumns( joinColumns, memberDetails, xmlDocumentContext ) );
}
if ( jaxbOverride.getJoinTable() != null ) {
annotationUsage.setAttributeValue(
"joinTable",
applyJoinTable( jaxbOverride.getJoinTable(), memberDetails, xmlDocumentContext )
TableProcessing.applyJoinTable( jaxbOverride.getJoinTable(), memberDetails, xmlDocumentContext )
);
}
if ( jaxbOverride.getForeignKeys() != null ) {
annotationUsage.setAttributeValue(
"foreignKey",
createForeignKeyAnnotation( jaxbOverride.getForeignKeys(), memberDetails, xmlDocumentContext )
ForeignKeyProcessing.createForeignKeyAnnotation( jaxbOverride.getForeignKeys(), memberDetails, xmlDocumentContext )
);
}
} );
@ -1602,7 +1417,7 @@ public class XmlAnnotationHelper {
);
final ClassDetails classDetails = xmlDocumentContext.getModelBuildingContext()
.getClassDetailsRegistry()
.findClassDetails( qualifiedName );
.resolveClassDetails( qualifiedName );
if ( classDetails != null ) {
return classDetails.getName();
}

View File

@ -21,6 +21,7 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbOrderColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPluralAttribute;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.internal.db.ForeignKeyProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.spi.AnnotationDescriptor;
@ -149,7 +150,7 @@ public class CommonPluralAttributeProcessing {
XmlAnnotationHelper.applyMapKeyJoinColumn( jaxbMapKeyJoinColumn, memberDetails, xmlDocumentContext );
} );
XmlAnnotationHelper.applyForeignKey( jaxbPluralAttribute.getMapKeyForeignKey(), memberDetails, xmlDocumentContext );
ForeignKeyProcessing.applyForeignKey( jaxbPluralAttribute.getMapKeyForeignKey(), memberDetails, xmlDocumentContext );
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// filters and custom sql

View File

@ -10,6 +10,8 @@ import org.hibernate.boot.jaxb.mapping.spi.JaxbCollectionTableImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollectionImpl;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.internal.db.ForeignKeyProcessing;
import org.hibernate.boot.models.xml.internal.db.JoinColumnProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.MutableAnnotationUsage;
@ -120,12 +122,12 @@ public class ElementCollectionAttributeProcessing {
);
XmlAnnotationHelper.applyOr( jaxbCollectionTable, JaxbCollectionTableImpl::getOptions, "options", collectionTableAnn, collectionTableDescriptor );
collectionTableAnn.setAttributeValue( "joinColumns", XmlAnnotationHelper.createJoinColumns( jaxbCollectionTable.getJoinColumns(), xmlDocumentContext ) );
collectionTableAnn.setAttributeValue( "joinColumns", JoinColumnProcessing.transformJoinColumnList( jaxbCollectionTable.getJoinColumns(), xmlDocumentContext ) );
if ( jaxbCollectionTable.getForeignKeys() != null ) {
collectionTableAnn.setAttributeValue(
"foreignKey",
XmlAnnotationHelper.createForeignKeyAnnotation( jaxbCollectionTable.getForeignKeys(), xmlDocumentContext )
ForeignKeyProcessing.createNestedForeignKeyAnnotation( jaxbCollectionTable.getForeignKeys(), memberDetails, xmlDocumentContext )
);
}

View File

@ -9,6 +9,7 @@ package org.hibernate.boot.models.xml.internal.attr;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManyToManyImpl;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.internal.db.TableProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.spi.MutableClassDetails;
@ -54,7 +55,7 @@ public class ManyToManyAttributeProcessing {
CommonPluralAttributeProcessing.applyPluralAttributeStructure( jaxbManyToMany, memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applyJoinTable( jaxbManyToMany.getJoinTable(), memberDetails, xmlDocumentContext );
TableProcessing.applyJoinTable( jaxbManyToMany.getJoinTable(), memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applySqlJoinTableRestriction( jaxbManyToMany.getSqlJoinTableRestriction(), memberDetails, xmlDocumentContext );

View File

@ -15,8 +15,10 @@ import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.boot.internal.Target;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOneImpl;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.internal.db.JoinColumnProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
@ -56,7 +58,8 @@ public class ManyToOneAttributeProcessing {
CommonAttributeProcessing.applyAttributeBasics( jaxbManyToOne, memberDetails, manyToOneAnn, accessType, xmlDocumentContext );
XmlAnnotationHelper.applyJoinColumns( jaxbManyToOne.getJoinColumns(), memberDetails, xmlDocumentContext );
JoinColumnProcessing.applyJoinColumns( jaxbManyToOne.getJoinColumns(), memberDetails, xmlDocumentContext );
applyNotFound( memberDetails, jaxbManyToOne, manyToOneAnn, xmlDocumentContext );
applyOnDelete( memberDetails, jaxbManyToOne, manyToOneAnn, xmlDocumentContext );
applyTarget( memberDetails, jaxbManyToOne, manyToOneAnn, xmlDocumentContext );
@ -69,21 +72,17 @@ public class ManyToOneAttributeProcessing {
MutableMemberDetails memberDetails,
JaxbManyToOneImpl jaxbManyToOne,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<ManyToOne> manyToOneAnn = XmlProcessingHelper.getOrMakeAnnotation( ManyToOne.class, memberDetails, xmlDocumentContext );
final AnnotationDescriptor<ManyToOne> manyToOneDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( ManyToOne.class );
XmlAnnotationHelper.applyOr(
jaxbManyToOne,
JaxbManyToOneImpl::getFetch,
final MutableAnnotationUsage<ManyToOne> manyToOneUsage = XmlProcessingHelper.getOrMakeAnnotation( ManyToOne.class, memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applyOptionalAttribute(
manyToOneUsage,
"fetch",
manyToOneAnn,
manyToOneDescriptor
jaxbManyToOne.getFetch()
);
return manyToOneAnn;
if ( jaxbManyToOne.isId() == Boolean.TRUE ) {
memberDetails.applyAnnotationUsage( JpaAnnotations.ID, xmlDocumentContext.getModelBuildingContext() );
}
return manyToOneUsage;
}
private static void applyNotFound(

View File

@ -12,6 +12,7 @@ import org.hibernate.annotations.OnDelete;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOneToManyImpl;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.internal.db.TableProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.spi.AnnotationDescriptor;
@ -55,7 +56,7 @@ public class OneToManyAttributeProcessing {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// join-table
XmlAnnotationHelper.applyJoinTable( jaxbOneToMany.getJoinTable(), memberDetails, xmlDocumentContext );
TableProcessing.applyJoinTable( jaxbOneToMany.getJoinTable(), memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applySqlJoinTableRestriction( jaxbOneToMany.getSqlJoinTableRestriction(), memberDetails, xmlDocumentContext );

View File

@ -8,11 +8,13 @@ package org.hibernate.boot.models.xml.internal.attr;
import org.hibernate.boot.internal.Target;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOneImpl;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.HibernateAnnotations;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.internal.db.JoinColumnProcessing;
import org.hibernate.boot.models.xml.internal.db.TableProcessing;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.MutableAnnotationUsage;
import org.hibernate.models.spi.MutableClassDetails;
import org.hibernate.models.spi.MutableMemberDetails;
@ -22,7 +24,6 @@ import jakarta.persistence.OneToOne;
import static org.hibernate.boot.models.xml.internal.XmlAnnotationHelper.applyCascading;
import static org.hibernate.boot.models.xml.internal.XmlAnnotationHelper.determineTargetName;
import static org.hibernate.boot.models.xml.internal.XmlProcessingHelper.getOrMakeAnnotation;
import static org.hibernate.boot.models.xml.internal.attr.CommonAttributeProcessing.applyAttributeBasics;
import static org.hibernate.internal.util.NullnessHelper.coalesce;
@ -50,12 +51,29 @@ public class OneToOneAttributeProcessing {
xmlDocumentContext
);
XmlAnnotationHelper.applyJoinTable( jaxbOneToOne.getJoinTable(), memberDetails, xmlDocumentContext );
applyAttributeBasics( jaxbOneToOne, memberDetails, oneToOneAnn, accessType, xmlDocumentContext );
applyTarget( memberDetails, jaxbOneToOne, oneToOneAnn, xmlDocumentContext );
applyCascading( jaxbOneToOne.getCascade(), memberDetails, xmlDocumentContext );
TableProcessing.applyJoinTable( jaxbOneToOne.getJoinTable(), memberDetails, xmlDocumentContext );
JoinColumnProcessing.applyJoinColumns( jaxbOneToOne.getJoinColumn(), memberDetails, xmlDocumentContext );
JoinColumnProcessing.applyPrimaryKeyJoinColumns( jaxbOneToOne.getPrimaryKeyJoinColumn(), memberDetails, xmlDocumentContext );
if ( jaxbOneToOne.isId() == Boolean.TRUE ) {
memberDetails.applyAnnotationUsage(
JpaAnnotations.ID,
xmlDocumentContext.getModelBuildingContext()
);
}
if ( StringHelper.isNotEmpty( jaxbOneToOne.getMapsId() ) ) {
memberDetails.applyAnnotationUsage(
JpaAnnotations.MAPS_ID,
(usage) -> usage.setAttributeValue( "value", jaxbOneToOne.getMapsId() ),
xmlDocumentContext.getModelBuildingContext()
);
}
return memberDetails;
}
@ -63,21 +81,24 @@ public class OneToOneAttributeProcessing {
MutableMemberDetails memberDetails,
JaxbOneToOneImpl jaxbOneToOne,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<OneToOne> oneToOneAnn = getOrMakeAnnotation( OneToOne.class, memberDetails, xmlDocumentContext );
final AnnotationDescriptor<OneToOne> oneToOneDescriptor = xmlDocumentContext
.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( OneToOne.class );
XmlAnnotationHelper.applyOr(
jaxbOneToOne,
JaxbOneToOneImpl::getFetch,
"fetch",
oneToOneAnn,
oneToOneDescriptor
return memberDetails.applyAnnotationUsage(
JpaAnnotations.ONE_TO_ONE,
(usage) -> {
if ( jaxbOneToOne.getFetch() != null ) {
usage.setAttributeValue( "fetch", jaxbOneToOne.getFetch() );
}
if ( jaxbOneToOne.isOptional() != null ) {
usage.setAttributeValue( "optional", jaxbOneToOne.isOptional() );
}
if ( StringHelper.isNotEmpty( jaxbOneToOne.getMappedBy() ) ) {
usage.setAttributeValue( "mappedBy", jaxbOneToOne.getMappedBy() );
}
if ( jaxbOneToOne.isOrphanRemoval() != null ) {
usage.setAttributeValue( "orphanRemoval", jaxbOneToOne.isOrphanRemoval() );
}
},
xmlDocumentContext.getModelBuildingContext()
);
return oneToOneAnn;
}
@SuppressWarnings("unused")
@ -91,8 +112,12 @@ public class OneToOneAttributeProcessing {
return;
}
final MutableAnnotationUsage<Target> targetAnn = XmlProcessingHelper.makeAnnotation( Target.class, memberDetails, xmlDocumentContext );
targetAnn.setAttributeValue( "value", determineTargetName( targetEntityName, xmlDocumentContext ) );
final MutableAnnotationUsage<Target> targetUsage = memberDetails.applyAnnotationUsage(
HibernateAnnotations.TARGET,
xmlDocumentContext.getModelBuildingContext()
);
final String targetName = determineTargetName( targetEntityName, xmlDocumentContext );
targetUsage.setAttributeValue( "value", targetName );
}
}

View File

@ -0,0 +1,72 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.models.xml.internal.db;
import org.hibernate.boot.jaxb.mapping.spi.JaxbForeignKeyImpl;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.models.spi.MemberDetails;
import org.hibernate.models.spi.MutableAnnotationUsage;
import org.hibernate.models.spi.MutableMemberDetails;
import jakarta.persistence.ForeignKey;
/**
* @author Steve Ebersole
*/
public class ForeignKeyProcessing {
public static void applyForeignKey(
JaxbForeignKeyImpl jaxbForeignKey,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbForeignKey == null ) {
return;
}
createForeignKeyAnnotation( jaxbForeignKey, memberDetails, xmlDocumentContext );
}
public static MutableAnnotationUsage<ForeignKey> createForeignKeyAnnotation(
JaxbForeignKeyImpl jaxbForeignKey,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbForeignKey == null ) {
return null;
}
return memberDetails.applyAnnotationUsage(
JpaAnnotations.FOREIGN_KEY,
(foreignKeyUsage) -> {
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "name", jaxbForeignKey.getName() );
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "value", jaxbForeignKey.getConstraintMode() );
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "foreignKeyDefinition", jaxbForeignKey.getForeignKeyDefinition() );
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "options", jaxbForeignKey.getOptions() );
},
xmlDocumentContext.getModelBuildingContext()
);
}
public static MutableAnnotationUsage<ForeignKey> createNestedForeignKeyAnnotation(
JaxbForeignKeyImpl jaxbForeignKey,
MemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
assert jaxbForeignKey != null;
final MutableAnnotationUsage<ForeignKey> foreignKeyUsage = JpaAnnotations.FOREIGN_KEY.createUsage(
memberDetails,
xmlDocumentContext.getModelBuildingContext()
);
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "name", jaxbForeignKey.getName() );
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "value", jaxbForeignKey.getConstraintMode() );
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "foreignKeyDefinition", jaxbForeignKey.getForeignKeyDefinition() );
XmlAnnotationHelper.applyOptionalAttribute( foreignKeyUsage, "options", jaxbForeignKey.getOptions() );
return foreignKeyUsage;
}
}

View File

@ -0,0 +1,184 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.models.xml.internal.db;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.boot.jaxb.mapping.spi.JaxbForeignKeyImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJoinColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPrimaryKeyJoinColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.db.JaxbColumnJoined;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.models.spi.AnnotationUsage;
import org.hibernate.models.spi.MutableAnnotationUsage;
import org.hibernate.models.spi.MutableMemberDetails;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.PrimaryKeyJoinColumns;
/**
* XML -> AnnotationUsage support for {@linkplain JaxbColumnJoined}
*
* @author Steve Ebersole
*/
public class JoinColumnProcessing {
/**
* Support for {@linkplain JaxbPrimaryKeyJoinColumnImpl} to {@linkplain PrimaryKeyJoinColumns} transformation
*
* @see JaxbPrimaryKeyJoinColumnImpl
*/
public static void applyPrimaryKeyJoinColumns(
List<JaxbPrimaryKeyJoinColumnImpl> jaxbJoinColumns,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return;
}
final MutableAnnotationUsage<PrimaryKeyJoinColumns> columnsUsage = memberDetails.applyAnnotationUsage(
JpaAnnotations.PRIMARY_KEY_JOIN_COLUMNS,
xmlDocumentContext.getModelBuildingContext()
);
final List<MutableAnnotationUsage<PrimaryKeyJoinColumn>> columnUsages = CollectionHelper.arrayList( jaxbJoinColumns.size() );
columnsUsage.setAttributeValue( "value", columnUsages );
jaxbJoinColumns.forEach( (jaxbJoinColumn) -> {
final MutableAnnotationUsage<PrimaryKeyJoinColumn> columnUsage = JpaAnnotations.PRIMARY_KEY_JOIN_COLUMN.createUsage(
memberDetails,
xmlDocumentContext.getModelBuildingContext()
);
columnUsages.add( columnUsage );
transferJoinColumn(
jaxbJoinColumn,
columnUsage,
memberDetails,
xmlDocumentContext
);
} );
}
private static void transferJoinColumn(
JaxbColumnJoined jaxbJoinColumn,
MutableAnnotationUsage<? extends Annotation> joinColumnUsage,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
ColumnProcessing.applyColumnDetails( jaxbJoinColumn, memberDetails, joinColumnUsage, xmlDocumentContext );
XmlAnnotationHelper.applyOptionalAttribute(
joinColumnUsage,
"referencedColumnName",
jaxbJoinColumn.getReferencedColumnName()
);
final JaxbForeignKeyImpl jaxbForeignKey = jaxbJoinColumn.getForeignKey();
if ( jaxbForeignKey != null ) {
joinColumnUsage.setAttributeValue(
"foreignKey",
ForeignKeyProcessing.createNestedForeignKeyAnnotation( jaxbForeignKey, memberDetails, xmlDocumentContext )
);
}
}
/**
* Support for {@linkplain JaxbJoinColumnImpl} to {@linkplain JoinColumn} transformation.
* Used when the List is the value of an annotation attribute other than its repetition container.
* For example, {@linkplain CollectionTable#joinColumns()}, {@linkplain JoinTable#joinColumns()} and
* {@linkplain JoinTable#inverseJoinColumns()}
*/
public static List<AnnotationUsage<JoinColumn>> transformJoinColumnList(
List<JaxbJoinColumnImpl> jaxbJoinColumns,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return Collections.emptyList();
}
final List<AnnotationUsage<JoinColumn>> joinColumns = new ArrayList<>( jaxbJoinColumns.size() );
jaxbJoinColumns.forEach( jaxbJoinColumn -> {
joinColumns.add( createJoinColumnAnnotation( jaxbJoinColumn, JoinColumn.class, xmlDocumentContext ) );
} );
return joinColumns;
}
public static <A extends Annotation> MutableAnnotationUsage<A> createJoinColumnAnnotation(
JaxbColumnJoined jaxbJoinColumn,
MutableMemberDetails memberDetails,
Class<A> annotationType,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<A> joinColumnAnn = XmlProcessingHelper.getOrMakeNamedAnnotation(
annotationType,
jaxbJoinColumn.getName(),
memberDetails,
xmlDocumentContext
);
transferJoinColumn( jaxbJoinColumn, joinColumnAnn, memberDetails, xmlDocumentContext );
return joinColumnAnn;
}
public static <A extends Annotation> MutableAnnotationUsage<A> createJoinColumnAnnotation(
JaxbColumnJoined jaxbJoinColumn,
Class<A> annotationType,
XmlDocumentContext xmlDocumentContext) {
final MutableAnnotationUsage<A> joinColumnAnn = XmlProcessingHelper.getOrMakeAnnotation( annotationType, xmlDocumentContext );
transferJoinColumn( jaxbJoinColumn, joinColumnAnn, null, xmlDocumentContext );
return joinColumnAnn;
}
public static List<AnnotationUsage<JoinColumn>> createJoinColumns(
List<JaxbJoinColumnImpl> jaxbJoinColumns,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return Collections.emptyList();
}
final List<AnnotationUsage<JoinColumn>> joinColumns = new ArrayList<>( jaxbJoinColumns.size() );
jaxbJoinColumns.forEach( jaxbJoinColumn -> {
final MutableAnnotationUsage<JoinColumn> joinColumnUsage = JpaAnnotations.JOIN_COLUMN.createUsage(
memberDetails,
xmlDocumentContext.getModelBuildingContext()
);
transferJoinColumn(
jaxbJoinColumn,
joinColumnUsage,
memberDetails,
xmlDocumentContext
);
joinColumns.add( joinColumnUsage );
} );
return joinColumns;
}
public static void applyJoinColumns(
List<JaxbJoinColumnImpl> jaxbJoinColumns,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( CollectionHelper.isEmpty( jaxbJoinColumns ) ) {
return;
}
if ( jaxbJoinColumns.size() == 1 ) {
XmlAnnotationHelper.applyJoinColumn( jaxbJoinColumns.get( 0 ), memberDetails, xmlDocumentContext );
}
else {
final MutableAnnotationUsage<JoinColumns> columnsAnn = XmlProcessingHelper.makeAnnotation(
JoinColumns.class,
memberDetails,
xmlDocumentContext
);
columnsAnn.setAttributeValue( "value", createJoinColumns( jaxbJoinColumns, memberDetails, xmlDocumentContext ) );
}
}
}

View File

@ -0,0 +1,67 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.boot.models.xml.internal.db;
import java.util.List;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJoinColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJoinTableImpl;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.models.spi.MutableAnnotationUsage;
import org.hibernate.models.spi.MutableMemberDetails;
import jakarta.persistence.JoinTable;
/**
* @author Steve Ebersole
*/
public class TableProcessing {
public static MutableAnnotationUsage<JoinTable> applyJoinTable(
JaxbJoinTableImpl jaxbJoinTable,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbJoinTable == null ) {
return null;
}
final MutableAnnotationUsage<JoinTable> joinTableUsage = memberDetails.applyAnnotationUsage(
JpaAnnotations.JOIN_TABLE,
xmlDocumentContext.getModelBuildingContext()
);
XmlAnnotationHelper.applyOptionalAttribute( joinTableUsage, "name", jaxbJoinTable.getName() );
XmlAnnotationHelper.applyTableAttributes( jaxbJoinTable, memberDetails, joinTableUsage, JpaAnnotations.JOIN_TABLE, xmlDocumentContext );
final List<JaxbJoinColumnImpl> joinColumns = jaxbJoinTable.getJoinColumn();
if ( CollectionHelper.isNotEmpty( joinColumns ) ) {
joinTableUsage.setAttributeValue( "joinColumns", JoinColumnProcessing.transformJoinColumnList( joinColumns, xmlDocumentContext ) );
}
final List<JaxbJoinColumnImpl> inverseJoinColumns = jaxbJoinTable.getInverseJoinColumn();
if ( CollectionHelper.isNotEmpty( inverseJoinColumns ) ) {
joinTableUsage.setAttributeValue( "inverseJoinColumns", JoinColumnProcessing.transformJoinColumnList( inverseJoinColumns, xmlDocumentContext ) );
}
if ( jaxbJoinTable.getForeignKey() != null ) {
joinTableUsage.setAttributeValue(
"foreignKey",
ForeignKeyProcessing.createNestedForeignKeyAnnotation( jaxbJoinTable.getForeignKey(), memberDetails, xmlDocumentContext )
);
}
if ( jaxbJoinTable.getInverseForeignKey() != null ) {
joinTableUsage.setAttributeValue(
"inverseForeignKey",
ForeignKeyProcessing.createNestedForeignKeyAnnotation( jaxbJoinTable.getInverseForeignKey(), memberDetails, xmlDocumentContext )
);
}
return joinTableUsage;
}
}

View File

@ -10,8 +10,6 @@ import java.util.Collection;
import java.util.Map;
import org.hibernate.Incubating;
import org.hibernate.Internal;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
@ -158,19 +156,6 @@ public interface BootstrapContext {
*/
Object getScanner();
/**
* Retrieve the Hibernate Commons Annotations {@link ReflectionManager}.
*
* @apiNote Supported for internal use only. This method will go away as
* we migrate away from Hibernate Commons Annotations to Jandex for
* annotation handling and XMl to annotation merging.
*
* @deprecated HCANN is deprecated in favor of hibernate-models
*/
@Internal
@Deprecated
ReflectionManager getReflectionManager();
/**
* Access to the Jandex index passed by call to
* {@link org.hibernate.boot.MetadataBuilder#applyIndexView(IndexView)}, if any.

View File

@ -16,7 +16,6 @@ import org.hibernate.DuplicateMappingException;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.annotations.CollectionTypeRegistration;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl;
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
import org.hibernate.boot.model.NamedEntityGraphDefinition;
@ -326,10 +325,6 @@ public interface InFlightMetadataCollector extends MetadataImplementor {
void addDefaultResultSetMapping(NamedResultSetMappingDescriptor definition);
void addDefaultNamedProcedureCall(NamedProcedureCallDefinitionImpl procedureCallDefinition);
AnnotatedClassType addClassType(XClass clazz);
AnnotatedClassType getClassType(XClass clazz);
AnnotatedClassType addClassType(ClassDetails classDetails);
AnnotatedClassType getClassType(ClassDetails classDetails);

View File

@ -325,11 +325,6 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
typeConfiguration.scope( this );
observer.sessionFactoryCreated( this );
// As last operation, delete all caches from ReflectionManager
// (not modelled as a listener as we want this to be last)
bootstrapContext.getReflectionManager().reset();
}
catch ( Exception e ) {
disintegrate( e, integratorObserver );

View File

@ -1,276 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.jpa.event.internal;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XMethod;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.jpa.event.spi.CallbackDefinition;
import org.hibernate.jpa.event.spi.CallbackType;
import org.hibernate.mapping.Property;
import org.hibernate.models.spi.AnnotationUsage;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.MethodDetails;
import org.hibernate.property.access.spi.Getter;
import org.jboss.logging.Logger;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.ExcludeDefaultListeners;
import jakarta.persistence.ExcludeSuperclassListeners;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.PersistenceException;
/**
* Resolves JPA callback definitions using a HCANN ReflectionManager.
* <p>
* "legacy" in that we want to move to Jandex instead.
*
* @author Steve Ebersole
*/
public final class CallbackDefinitionResolverLegacyImpl {
private static final Logger log = Logger.getLogger( CallbackDefinitionResolverLegacyImpl.class );
public static List<CallbackDefinition> resolveEntityCallbacks(
ReflectionManager reflectionManager,
ClassDetails entityClass,
CallbackType callbackType) {
List<CallbackDefinition> callbackDefinitions = new ArrayList<>();
List<String> callbacksMethodNames = new ArrayList<>();
List<Class<?>> orderedListeners = new ArrayList<>();
ClassDetails currentClazz = entityClass;
boolean stopListeners = false;
boolean stopDefaultListeners = false;
do {
CallbackDefinition callbackDefinition = null;
final List<MethodDetails> methodsDetailsList = currentClazz.getMethods();
for ( MethodDetails methodDetails : methodsDetailsList ) {
if ( !methodDetails.hasAnnotationUsage( callbackType.getCallbackAnnotation() ) ) {
continue;
}
if ( callbacksMethodNames.contains( methodDetails.getName() ) ) {
continue;
}
//overridden method, remove the superclass overridden method
if ( callbackDefinition == null ) {
final Method javaMethod = (Method) methodDetails.toJavaMember();
callbackDefinition = new EntityCallback.Definition( javaMethod, callbackType );
Class<?> returnType = javaMethod.getReturnType();
Class<?>[] args = javaMethod.getParameterTypes();
if ( returnType != Void.TYPE || args.length != 0 ) {
throw new RuntimeException(
"Callback methods annotated on the bean class must return void and take no arguments: "
+ callbackType.getCallbackAnnotation().getName() + " - " + methodDetails
);
}
ReflectHelper.ensureAccessibility( javaMethod );
if ( log.isDebugEnabled() ) {
log.debugf(
"Adding %s as %s callback for entity %s",
methodDetails.getName(),
callbackType.getCallbackAnnotation().getSimpleName(),
entityClass.getName()
);
}
callbackDefinitions.add( 0, callbackDefinition ); //superclass first
callbacksMethodNames.add( 0, methodDetails.getName() );
}
else {
throw new PersistenceException(
"You can only annotate one callback method with "
+ callbackType.getCallbackAnnotation().getName() + " in bean class: " + entityClass.getName()
);
}
}
if ( !stopListeners ) {
applyListeners( currentClazz, orderedListeners );
stopListeners = currentClazz.hasAnnotationUsage( ExcludeSuperclassListeners.class );
stopDefaultListeners = currentClazz.hasAnnotationUsage( ExcludeDefaultListeners.class );
}
do {
currentClazz = currentClazz.getSuperClass();
}
while ( currentClazz != null
&& !( currentClazz.hasAnnotationUsage( Entity.class )
|| currentClazz.hasAnnotationUsage( MappedSuperclass.class ) )
);
}
while ( currentClazz != null );
//handle default listeners
if ( !stopDefaultListeners ) {
@SuppressWarnings("unchecked")
List<Class<?>> defaultListeners = (List<Class<?>>)
reflectionManager.getDefaults().get( EntityListeners.class );
if ( defaultListeners != null ) {
int defaultListenerSize = defaultListeners.size();
for ( int i = defaultListenerSize - 1; i >= 0; i-- ) {
orderedListeners.add( defaultListeners.get( i ) );
}
}
}
for ( Class<?> listener : orderedListeners ) {
CallbackDefinition callbackDefinition = null;
if ( listener != null ) {
XClass xListener = reflectionManager.toXClass( listener );
callbacksMethodNames = new ArrayList<>();
List<XMethod> methods = xListener.getDeclaredMethods();
for ( final XMethod xMethod : methods ) {
if ( xMethod.isAnnotationPresent( callbackType.getCallbackAnnotation() ) ) {
final Method method = reflectionManager.toMethod( xMethod );
final String methodName = method.getName();
if ( !callbacksMethodNames.contains( methodName ) ) {
//overridden method, remove the superclass overridden method
if ( callbackDefinition == null ) {
callbackDefinition = new ListenerCallback.Definition( listener, method, callbackType );
Class<?> returnType = method.getReturnType();
Class<?>[] args = method.getParameterTypes();
if ( returnType != Void.TYPE || args.length != 1 ) {
throw new PersistenceException(
"Callback methods annotated in a listener bean class must return void and take one argument: "
+ callbackType.getCallbackAnnotation().getName() + " - " + method
);
}
ReflectHelper.ensureAccessibility( method );
if ( log.isDebugEnabled() ) {
log.debugf(
"Adding %s as %s callback for entity %s",
methodName,
callbackType.getCallbackAnnotation().getSimpleName(),
entityClass.getName()
);
}
callbackDefinitions.add( 0, callbackDefinition ); // listeners first
}
else {
throw new PersistenceException(
"You can only annotate one callback method with "
+ callbackType.getCallbackAnnotation().getName()
+ " in bean class: " + entityClass.getName()
+ " and callback listener: " + listener.getName()
);
}
}
}
}
}
}
return callbackDefinitions;
}
public static List<CallbackDefinition> resolveEmbeddableCallbacks(
ReflectionManager reflectionManager,
Class<?> entityClass,
Property embeddableProperty,
CallbackType callbackType) {
final Class<?> embeddableClass = embeddableProperty.getType().getReturnedClass();
final XClass embeddableXClass = reflectionManager.toXClass( embeddableClass );
final Getter embeddableGetter = embeddableProperty.getGetter( entityClass );
final List<CallbackDefinition> callbackDefinitions = new ArrayList<>();
final List<String> callbacksMethodNames = new ArrayList<>();
XClass currentClazz = embeddableXClass;
do {
CallbackDefinition callbackDefinition = null;
List<XMethod> methods = currentClazz.getDeclaredMethods();
for ( final XMethod xMethod : methods ) {
if ( xMethod.isAnnotationPresent( callbackType.getCallbackAnnotation() ) ) {
Method method = reflectionManager.toMethod( xMethod );
final String methodName = method.getName();
if ( !callbacksMethodNames.contains( methodName ) ) {
//overridden method, remove the superclass overridden method
if ( callbackDefinition == null ) {
callbackDefinition = new EmbeddableCallback.Definition( embeddableGetter, method, callbackType );
Class<?> returnType = method.getReturnType();
Class<?>[] args = method.getParameterTypes();
if ( returnType != Void.TYPE || args.length != 0 ) {
throw new RuntimeException(
"Callback methods annotated on the bean class must return void and take no arguments: "
+ callbackType.getCallbackAnnotation().getName() + " - " + xMethod
);
}
ReflectHelper.ensureAccessibility( method );
if ( log.isDebugEnabled() ) {
log.debugf(
"Adding %s as %s callback for entity %s",
methodName,
callbackType.getCallbackAnnotation().getSimpleName(),
embeddableXClass.getName()
);
}
callbackDefinitions.add( 0, callbackDefinition ); //superclass first
callbacksMethodNames.add( 0, methodName );
}
else {
throw new PersistenceException(
"You can only annotate one callback method with "
+ callbackType.getCallbackAnnotation().getName() + " in bean class: " + embeddableXClass.getName()
);
}
}
}
}
do {
currentClazz = currentClazz.getSuperclass();
}
while ( currentClazz != null && !currentClazz.isAnnotationPresent( MappedSuperclass.class ) );
}
while ( currentClazz != null );
return callbackDefinitions;
}
private static boolean useAnnotationAnnotatedByListener;
static {
//check whether reading annotations of annotations is useful or not
useAnnotationAnnotatedByListener = false;
Target target = EntityListeners.class.getAnnotation( Target.class );
if ( target != null ) {
for ( ElementType type : target.value() ) {
if ( type.equals( ElementType.ANNOTATION_TYPE ) ) {
useAnnotationAnnotatedByListener = true;
break;
}
}
}
}
private static void applyListeners(ClassDetails currentClazz, List<Class<?>> listOfListeners) {
final AnnotationUsage<EntityListeners> entityListeners = currentClazz.getAnnotationUsage( EntityListeners.class );
if ( entityListeners != null ) {
final List<ClassDetails> listeners = entityListeners.getList( "value" );
for ( ClassDetails listener : listeners ) {
listOfListeners.add( listener.toJavaClass() );
}
}
if ( useAnnotationAnnotatedByListener ) {
final List<AnnotationUsage<?>> metaAnnotatedUsageList = currentClazz.getMetaAnnotated( EntityListeners.class );
for ( AnnotationUsage<?> metaAnnotatedUsage : metaAnnotatedUsageList ) {
final AnnotationUsage<EntityListeners> metaAnnotatedListeners = metaAnnotatedUsage.getAnnotationDescriptor().getAnnotationUsage( EntityListeners.class );
final List<ClassDetails> listeners = metaAnnotatedListeners.getList( "value" );
for ( ClassDetails listener : listeners ) {
listOfListeners.add( listener.toJavaClass() );
}
}
}
}
}

View File

@ -1,469 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.annotations.reflection;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.hibernate.annotations.Columns;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.model.internal.JPAXMLOverriddenAnnotationReader;
import org.hibernate.boot.model.internal.XMLContext;
import org.hibernate.orm.test.internal.util.xml.XMLMappingHelper;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.boot.BootstrapContextImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import jakarta.persistence.AssociationOverrides;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.Converts;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.ExcludeDefaultListeners;
import jakarta.persistence.ExcludeSuperclassListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.MapKey;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.NamedNativeQueries;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.OrderBy;
import jakarta.persistence.PostLoad;
import jakarta.persistence.PostPersist;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.PrimaryKeyJoinColumns;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.SecondaryTables;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.SqlResultSetMappings;
import jakarta.persistence.Table;
import jakarta.persistence.TableGenerator;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.persistence.Transient;
import jakarta.persistence.Version;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
@TestForIssue(jiraKey = "HHH-14529")
public class JPAXMLOverriddenAnnotationReaderTest extends BaseUnitTestCase {
private BootstrapContextImpl bootstrapContext;
@Before
public void init() {
bootstrapContext = new BootstrapContextImpl();
}
@After
public void destroy() {
bootstrapContext.close();
}
@Test
public void testMappedSuperclassAnnotations() throws Exception {
XMLContext context = buildContext(
"org/hibernate/orm/test/annotations/reflection/metadata-complete.xml"
);
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( Organization.class, context, bootstrapContext );
assertTrue( reader.isAnnotationPresent( MappedSuperclass.class ) );
}
@Test
public void testEntityRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( Administration.class, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Entity.class ) );
assertEquals(
"Default value in xml entity should not override @Entity.name", "JavaAdministration",
reader.getAnnotation( Entity.class ).name()
);
assertNotNull( reader.getAnnotation( Table.class ) );
assertEquals( "@Table not overridden", "tbl_admin", reader.getAnnotation( Table.class ).name() );
// The default schema is assigned later, when we generate SQL.
// See DefaultCatalogAndSchemaTest.
assertEquals( "Default schema overridden too soon", "", reader.getAnnotation( Table.class ).schema() );
assertEquals(
"Proper @Table.uniqueConstraints", 2,
reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames().length
);
String columnName = reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames()[0];
assertTrue(
"Proper @Table.uniqueConstraints", "firstname".equals( columnName ) || "lastname".equals( columnName )
);
assertNull( "Both Java and XML used", reader.getAnnotation( SecondaryTable.class ) );
assertNotNull( "XML does not work", reader.getAnnotation( SecondaryTables.class ) );
SecondaryTable[] tables = reader.getAnnotation( SecondaryTables.class ).value();
assertEquals( 1, tables.length );
assertEquals( "admin2", tables[0].name() );
assertEquals( "unique constraints ignored", 1, tables[0].uniqueConstraints().length );
assertEquals( "pk join column ignored", 1, tables[0].pkJoinColumns().length );
assertEquals( "pk join column ignored", "admin_id", tables[0].pkJoinColumns()[0].name() );
assertNotNull( "Sequence Overriding not working", reader.getAnnotation( SequenceGenerator.class ) );
assertEquals(
"wrong sequence name", "seqhilo", reader.getAnnotation( SequenceGenerator.class ).sequenceName()
);
assertEquals( "default fails", 50, reader.getAnnotation( SequenceGenerator.class ).allocationSize() );
assertNotNull( "TableOverriding not working", reader.getAnnotation( TableGenerator.class ) );
assertEquals( "wrong tble name", "tablehilo", reader.getAnnotation( TableGenerator.class ).table() );
// The default schema is assigned later, when we generate SQL.
// See DefaultCatalogAndSchemaTest.
assertEquals( "Default schema overridden too soon", "", reader.getAnnotation( TableGenerator.class ).schema() );
reader = new JPAXMLOverriddenAnnotationReader( Match.class, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Table.class ) );
assertEquals(
"Java annotation not taken into account", "matchtable", reader.getAnnotation( Table.class ).name()
);
assertEquals(
"Java annotation not taken into account", "matchschema", reader.getAnnotation( Table.class ).schema()
);
// The default schema is assigned later, when we generate SQL.
// See DefaultCatalogAndSchemaTest.
assertEquals( "Default catalog overridden too soon", "", reader.getAnnotation( Table.class ).catalog() );
assertNotNull( "SecondaryTable swallowed", reader.getAnnotation( SecondaryTables.class ) );
// The default schema is assigned later, when we generate SQL.
// See DefaultCatalogAndSchemaTest.
assertEquals(
"Default schema not taken into account", "",
reader.getAnnotation( SecondaryTables.class ).value()[0].schema()
);
assertNotNull( reader.getAnnotation( Inheritance.class ) );
assertEquals(
"inheritance strategy not overridden", InheritanceType.JOINED,
reader.getAnnotation( Inheritance.class ).strategy()
);
assertNotNull( "NamedQuery not overridden", reader.getAnnotation( NamedQueries.class ) );
assertEquals( "No deduplication", 3, reader.getAnnotation( NamedQueries.class ).value().length );
assertEquals(
"deduplication kept the Java version", 1,
reader.getAnnotation( NamedQueries.class ).value()[1].hints().length
);
assertEquals(
"org.hibernate.timeout", reader.getAnnotation( NamedQueries.class ).value()[1].hints()[0].name()
);
assertNotNull( "NamedNativeQuery not overridden", reader.getAnnotation( NamedNativeQueries.class ) );
assertEquals( "No deduplication", 3, reader.getAnnotation( NamedNativeQueries.class ).value().length );
assertEquals(
"deduplication kept the Java version", 1,
reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints().length
);
assertEquals(
"org.hibernate.timeout", reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints()[0].name()
);
assertNotNull( reader.getAnnotation( SqlResultSetMappings.class ) );
assertEquals(
"competitor1Point", reader.getAnnotation( SqlResultSetMappings.class ).value()[0].columns()[0].name()
);
assertEquals(
"competitor1Point",
reader.getAnnotation( SqlResultSetMappings.class ).value()[0].entities()[0].fields()[0].column()
);
assertNotNull( reader.getAnnotation( ExcludeSuperclassListeners.class ) );
assertNotNull( reader.getAnnotation( ExcludeDefaultListeners.class ) );
reader = new JPAXMLOverriddenAnnotationReader( Competition.class, context, bootstrapContext );
assertNotNull( reader.getAnnotation( MappedSuperclass.class ) );
reader = new JPAXMLOverriddenAnnotationReader( TennisMatch.class, context, bootstrapContext );
assertNull( "Mutualize PKJC into PKJCs", reader.getAnnotation( PrimaryKeyJoinColumn.class ) );
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
assertEquals(
"PrimaryKeyJoinColumn overrden", "id",
reader.getAnnotation( PrimaryKeyJoinColumns.class ).value()[0].name()
);
assertNotNull( reader.getAnnotation( AttributeOverrides.class ) );
assertEquals( "Wrong deduplication", 3, reader.getAnnotation( AttributeOverrides.class ).value().length );
assertEquals(
"Wrong priority (XML vs java annotations)", "fld_net",
reader.getAnnotation( AttributeOverrides.class ).value()[0].column().name()
);
assertEquals(
"Column mapping", 2, reader.getAnnotation( AttributeOverrides.class ).value()[1].column().scale()
);
assertEquals(
"Column mapping", true, reader.getAnnotation( AttributeOverrides.class ).value()[1].column().unique()
);
assertNotNull( reader.getAnnotation( AssociationOverrides.class ) );
assertEquals( "no XML processing", 1, reader.getAnnotation( AssociationOverrides.class ).value().length );
assertEquals(
"wrong xml processing", "id",
reader.getAnnotation( AssociationOverrides.class ).value()[0].joinColumns()[0].referencedColumnName()
);
reader = new JPAXMLOverriddenAnnotationReader( SocialSecurityPhysicalAccount.class, context, bootstrapContext );
assertNotNull( reader.getAnnotation( IdClass.class ) );
assertEquals( "id-class not used", SocialSecurityNumber.class, reader.getAnnotation( IdClass.class ).value() );
assertEquals(
"discriminator-value not used", "Physical", reader.getAnnotation( DiscriminatorValue.class ).value()
);
assertNotNull( "discriminator-column not used", reader.getAnnotation( DiscriminatorColumn.class ) );
assertEquals(
"discriminator-column.name default value broken", "DTYPE",
reader.getAnnotation( DiscriminatorColumn.class ).name()
);
assertEquals(
"discriminator-column.length broken", 34, reader.getAnnotation( DiscriminatorColumn.class ).length()
);
}
@Test
public void testEntityRelatedAnnotationsMetadataComplete() throws Exception {
XMLContext context = buildContext(
"org/hibernate/orm/test/annotations/reflection/metadata-complete.xml"
);
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( Administration.class, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Entity.class ) );
assertEquals(
"Metadata complete should ignore java annotations", "", reader.getAnnotation( Entity.class ).name()
);
// The default schema is assigned later, when we generate SQL.
// See DefaultCatalogAndSchemaTest.
assertNull( "Default schema overridden too soon", reader.getAnnotation( Table.class ) );
reader = new JPAXMLOverriddenAnnotationReader( Match.class, context, bootstrapContext );
// The default schema is assigned later, when we generate SQL.
// See DefaultCatalogAndSchemaTest.
assertNull( "Default schema overridden too soon", reader.getAnnotation( Table.class ) );
assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTable.class ) );
assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTables.class ) );
assertNull( "Ignore Java annotation", reader.getAnnotation( Inheritance.class ) );
assertNull( reader.getAnnotation( NamedQueries.class ) );
assertNull( reader.getAnnotation( NamedNativeQueries.class ) );
reader = new JPAXMLOverriddenAnnotationReader( TennisMatch.class, context, bootstrapContext );
assertNull( reader.getAnnotation( PrimaryKeyJoinColumn.class ) );
assertNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
reader = new JPAXMLOverriddenAnnotationReader( Competition.class, context, bootstrapContext );
assertNull( reader.getAnnotation( MappedSuperclass.class ) );
reader = new JPAXMLOverriddenAnnotationReader( SocialSecurityMoralAccount.class, context, bootstrapContext );
assertNull( reader.getAnnotation( IdClass.class ) );
assertNull( reader.getAnnotation( DiscriminatorValue.class ) );
assertNull( reader.getAnnotation( DiscriminatorColumn.class ) );
assertNull( reader.getAnnotation( SequenceGenerator.class ) );
assertNull( reader.getAnnotation( TableGenerator.class ) );
}
@Test
public void testIdRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "getId" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertNull( reader.getAnnotation( Id.class ) );
assertNull( reader.getAnnotation( Column.class ) );
Field field = Administration.class.getDeclaredField( "id" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Id.class ) );
assertNotNull( reader.getAnnotation( GeneratedValue.class ) );
assertEquals( GenerationType.SEQUENCE, reader.getAnnotation( GeneratedValue.class ).strategy() );
assertEquals( "generator", reader.getAnnotation( GeneratedValue.class ).generator() );
assertNotNull( reader.getAnnotation( SequenceGenerator.class ) );
assertEquals( "seq", reader.getAnnotation( SequenceGenerator.class ).sequenceName() );
assertNotNull( reader.getAnnotation( Columns.class ) );
assertEquals( 1, reader.getAnnotation( Columns.class ).columns().length );
assertEquals( "fld_id", reader.getAnnotation( Columns.class ).columns()[0].name() );
assertNotNull( reader.getAnnotation( Temporal.class ) );
assertEquals( TemporalType.DATE, reader.getAnnotation( Temporal.class ).value() );
context = buildContext(
"org/hibernate/orm/test/annotations/reflection/metadata-complete.xml"
);
method = Administration.class.getDeclaredMethod( "getId" );
reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertNotNull(
"Default access type when not defined in metadata complete should be property",
reader.getAnnotation( Id.class )
);
field = Administration.class.getDeclaredField( "id" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNull(
"Default access type when not defined in metadata complete should be property",
reader.getAnnotation( Id.class )
);
method = BusTrip.class.getDeclaredMethod( "getId" );
reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertNull( reader.getAnnotation( EmbeddedId.class ) );
field = BusTrip.class.getDeclaredField( "id" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( EmbeddedId.class ) );
assertNotNull( reader.getAnnotation( AttributeOverrides.class ) );
assertEquals( 1, reader.getAnnotation( AttributeOverrides.class ).value().length );
}
@Test
public void testBasicRelatedAnnotations() throws Exception {
XMLContext context = buildContext(
"org/hibernate/orm/test/annotations/reflection/metadata-complete.xml"
);
Field field = BusTrip.class.getDeclaredField( "status" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Enumerated.class ) );
assertEquals( EnumType.STRING, reader.getAnnotation( Enumerated.class ).value() );
assertEquals( false, reader.getAnnotation( Basic.class ).optional() );
field = BusTrip.class.getDeclaredField( "serial" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Lob.class ) );
assertEquals( "serialbytes", reader.getAnnotation( Columns.class ).columns()[0].name() );
field = BusTrip.class.getDeclaredField( "terminusTime" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Temporal.class ) );
assertEquals( TemporalType.TIMESTAMP, reader.getAnnotation( Temporal.class ).value() );
assertEquals( FetchType.LAZY, reader.getAnnotation( Basic.class ).fetch() );
field = BusTripPk.class.getDeclaredField( "busDriver" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertTrue( reader.isAnnotationPresent( Basic.class ) );
}
@Test
public void testVersionRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "getVersion" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Version.class ) );
Field field = Match.class.getDeclaredField( "version" );
assertNotNull( reader.getAnnotation( Version.class ) );
}
@Test
public void testTransientAndEmbeddedRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
Field field = Administration.class.getDeclaredField( "transientField" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Transient.class ) );
assertNull( reader.getAnnotation( Basic.class ) );
field = Match.class.getDeclaredField( "playerASSN" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( Embedded.class ) );
}
@Test
public void testAssociationRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
Field field = Administration.class.getDeclaredField( "defaultBusTrip" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( OneToOne.class ) );
assertNull( reader.getAnnotation( JoinColumns.class ) );
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
assertEquals( "pk", reader.getAnnotation( PrimaryKeyJoinColumns.class ).value()[0].name() );
assertEquals( 5, reader.getAnnotation( OneToOne.class ).cascade().length );
assertEquals( FetchType.LAZY, reader.getAnnotation( OneToOne.class ).fetch() );
assertEquals( "test", reader.getAnnotation( OneToOne.class ).mappedBy() );
context = buildContext(
"org/hibernate/orm/test/annotations/reflection/metadata-complete.xml"
);
field = BusTrip.class.getDeclaredField( "players" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( OneToMany.class ) );
assertNotNull( reader.getAnnotation( JoinColumns.class ) );
assertEquals( 2, reader.getAnnotation( JoinColumns.class ).value().length );
assertEquals( "driver", reader.getAnnotation( JoinColumns.class ).value()[0].name() );
assertNotNull( reader.getAnnotation( MapKey.class ) );
assertEquals( "name", reader.getAnnotation( MapKey.class ).name() );
field = BusTrip.class.getDeclaredField( "roads" );
reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( ManyToMany.class ) );
assertNotNull( reader.getAnnotation( JoinTable.class ) );
assertEquals( "bus_road", reader.getAnnotation( JoinTable.class ).name() );
assertEquals( 2, reader.getAnnotation( JoinTable.class ).joinColumns().length );
assertEquals( 1, reader.getAnnotation( JoinTable.class ).inverseJoinColumns().length );
assertEquals( 2, reader.getAnnotation( JoinTable.class ).uniqueConstraints()[0].columnNames().length );
assertNotNull( reader.getAnnotation( OrderBy.class ) );
assertEquals( "maxSpeed", reader.getAnnotation( OrderBy.class ).value() );
}
@Test
@TestForIssue(jiraKey = "HHH-11924")
public void testElementCollectionConverter() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
Field field = Company.class.getDeclaredField( "organizations" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( field, context, bootstrapContext );
assertNotNull( reader.getAnnotation( ElementCollection.class ) );
assertNotNull( reader.getAnnotation( Converts.class ) );
assertNotNull( reader.getAnnotation( Converts.class ).value() );
assertTrue( reader.getAnnotation( Converts.class ).value().length == 1 );
assertEquals(OrganizationConverter.class, reader.getAnnotation( Converts.class ).value()[0].converter());
}
@Test
public void testEntityListeners() throws Exception {
XMLContext context = buildContext( "org/hibernate/orm/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "calculate" );
JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) );
reader = new JPAXMLOverriddenAnnotationReader( Administration.class, context, bootstrapContext );
assertTrue( reader.isAnnotationPresent( EntityListeners.class ) );
assertEquals( 1, reader.getAnnotation( EntityListeners.class ).value().length );
assertEquals( LogListener.class, reader.getAnnotation( EntityListeners.class ).value()[0] );
method = LogListener.class.getDeclaredMethod( "noLog", Object.class );
reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertTrue( reader.isAnnotationPresent( PostLoad.class ) );
method = LogListener.class.getDeclaredMethod( "log", Object.class );
reader = new JPAXMLOverriddenAnnotationReader( method, context, bootstrapContext );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) );
assertFalse( reader.isAnnotationPresent( PostPersist.class ) );
assertEquals( 1, context.getDefaultEntityListeners().size() );
assertEquals( OtherLogListener.class.getName(), context.getDefaultEntityListeners().get( 0 ) );
}
private XMLContext buildContext(String ormfile) throws IOException {
XMLMappingHelper xmlHelper = new XMLMappingHelper();
JaxbEntityMappingsImpl mappings = xmlHelper.readOrmXmlMappings( ormfile );
XMLContext context = new XMLContext( bootstrapContext );
context.addDocument( mappings );
return context;
}
}

View File

@ -30,7 +30,6 @@ import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.models.spi.FieldDetails;
import org.hibernate.usertype.DynamicParameterizedType;

View File

@ -1,708 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.xml.ejb3;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.AssociationOverride;
import jakarta.persistence.AssociationOverrides;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.Lob;
import jakarta.persistence.MapKey;
import jakarta.persistence.MapKeyClass;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.MapKeyEnumerated;
import jakarta.persistence.MapKeyJoinColumn;
import jakarta.persistence.MapKeyJoinColumns;
import jakarta.persistence.MapKeyTemporal;
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.persistence.UniqueConstraint;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@TestForIssue(jiraKey = "HHH-14529")
public class Ejb3XmlElementCollectionTest extends Ejb3XmlTestCase {
@Test
public void testNoChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "element-collection.orm1.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( Column.class );
assertAnnotationNotPresent( Temporal.class );
assertAnnotationNotPresent( Enumerated.class );
assertAnnotationNotPresent( Lob.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationNotPresent( AttributeOverrides.class );
assertAnnotationNotPresent( AssociationOverride.class );
assertAnnotationNotPresent( AssociationOverrides.class );
assertAnnotationNotPresent( CollectionTable.class );
assertAnnotationNotPresent( Access.class );
ElementCollection relAnno = reader.getAnnotation( ElementCollection.class );
assertEquals( FetchType.LAZY, relAnno.fetch() );
assertEquals( void.class, relAnno.targetClass() );
}
@Test
public void testOrderBy() throws Exception {
reader = getReader( Entity2.class, "field1", "element-collection.orm2.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertEquals(
"col1 ASC, col2 DESC", reader.getAnnotation( OrderBy.class )
.value()
);
}
@Test
public void testOrderColumnNoAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "element-collection.orm3.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationPresent( OrderColumn.class );
OrderColumn orderColumnAnno = reader.getAnnotation( OrderColumn.class );
assertEquals( "", orderColumnAnno.columnDefinition() );
assertEquals( "", orderColumnAnno.name() );
assertTrue( orderColumnAnno.insertable() );
assertTrue( orderColumnAnno.nullable() );
assertTrue( orderColumnAnno.updatable() );
}
@Test
public void testOrderColumnAllAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "element-collection.orm4.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationPresent( OrderColumn.class );
OrderColumn orderColumnAnno = reader.getAnnotation( OrderColumn.class );
assertEquals( "int", orderColumnAnno.columnDefinition() );
assertEquals( "col1", orderColumnAnno.name() );
assertFalse( orderColumnAnno.insertable() );
assertFalse( orderColumnAnno.nullable() );
assertFalse( orderColumnAnno.updatable() );
}
@Test
public void testMapKeyNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm5.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals( "", reader.getAnnotation( MapKey.class ).name() );
}
@Test
public void testMapKeyAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm6.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals( "field2", reader.getAnnotation( MapKey.class ).name() );
}
@Test
public void testMapKeyClass() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm7.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
Entity2.class, reader.getAnnotation( MapKeyClass.class )
.value()
);
}
@Test
public void testMapKeyTemporal() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm8.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
TemporalType.DATE, reader.getAnnotation(
MapKeyTemporal.class
).value()
);
}
@Test
public void testMapKeyEnumerated() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm9.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
EnumType.STRING, reader.getAnnotation(
MapKeyEnumerated.class
).value()
);
}
/**
* When there's a single map key attribute override, we still wrap it with
* an AttributeOverrides annotation.
*/
@Test
public void testSingleMapKeyAttributeOverride() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm10.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 1, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "col1", overrides[0].column().name() );
}
@Test
public void testMultipleMapKeyAttributeOverrides() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm11.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 2, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "", overrides[0].column().name() );
assertFalse( overrides[0].column().unique() );
assertTrue( overrides[0].column().nullable() );
assertTrue( overrides[0].column().insertable() );
assertTrue( overrides[0].column().updatable() );
assertEquals( "", overrides[0].column().columnDefinition() );
assertEquals( "", overrides[0].column().table() );
assertEquals( 255, overrides[0].column().length() );
assertEquals( 0, overrides[0].column().precision() );
assertEquals( 0, overrides[0].column().scale() );
assertEquals( "field2", overrides[1].name() );
assertEquals( "col1", overrides[1].column().name() );
assertTrue( overrides[1].column().unique() );
assertFalse( overrides[1].column().nullable() );
assertFalse( overrides[1].column().insertable() );
assertFalse( overrides[1].column().updatable() );
assertEquals( "int", overrides[1].column().columnDefinition() );
assertEquals( "table1", overrides[1].column().table() );
assertEquals( 50, overrides[1].column().length() );
assertEquals( 2, overrides[1].column().precision() );
assertEquals( 1, overrides[1].column().scale() );
}
@Test
public void testMapKeyColumnNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm12.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyColumn keyColAnno = reader.getAnnotation( MapKeyColumn.class );
assertEquals( "", keyColAnno.columnDefinition() );
assertEquals( "", keyColAnno.name() );
assertEquals( "", keyColAnno.table() );
assertFalse( keyColAnno.nullable() );
assertTrue( keyColAnno.insertable() );
assertFalse( keyColAnno.unique() );
assertTrue( keyColAnno.updatable() );
assertEquals( 255, keyColAnno.length() );
assertEquals( 0, keyColAnno.precision() );
assertEquals( 0, keyColAnno.scale() );
}
@Test
public void testMapKeyColumnAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm13.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyColumn keyColAnno = reader.getAnnotation( MapKeyColumn.class );
assertEquals( "int", keyColAnno.columnDefinition() );
assertEquals( "col1", keyColAnno.name() );
assertEquals( "table1", keyColAnno.table() );
assertTrue( keyColAnno.nullable() );
assertFalse( keyColAnno.insertable() );
assertTrue( keyColAnno.unique() );
assertFalse( keyColAnno.updatable() );
assertEquals( 50, keyColAnno.length() );
assertEquals( 2, keyColAnno.precision() );
assertEquals( 1, keyColAnno.scale() );
}
/**
* When there's a single map key join column, we still wrap it with a
* MapKeyJoinColumns annotation.
*/
@Test
public void testSingleMapKeyJoinColumn() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm14.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyJoinColumns joinColumnsAnno = reader
.getAnnotation( MapKeyJoinColumns.class );
MapKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
}
@Test
public void testMultipleMapKeyJoinColumns() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm15.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyJoinColumns joinColumnsAnno = reader
.getAnnotation( MapKeyJoinColumns.class );
MapKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertFalse( joinColumns[0].unique() );
assertFalse( joinColumns[0].nullable() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertTrue( joinColumns[1].unique() );
assertTrue( joinColumns[1].nullable() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertEquals( "table1", joinColumns[1].table() );
}
@Test
public void testColumnNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm16.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( Column.class );
Column column = reader.getAnnotation( Column.class );
assertEquals( "", column.name() );
assertFalse( column.unique() );
assertTrue( column.nullable() );
assertTrue( column.insertable() );
assertTrue( column.updatable() );
assertEquals( "", column.columnDefinition() );
assertEquals( "", column.table() );
assertEquals( 255, column.length() );
assertEquals( 0, column.precision() );
assertEquals( 0, column.scale() );
}
@Test
public void testColumnAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm17.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( Column.class );
Column column = reader.getAnnotation( Column.class );
assertEquals( "col1", column.name() );
assertTrue( column.unique() );
assertFalse( column.nullable() );
assertFalse( column.insertable() );
assertFalse( column.updatable() );
assertEquals( "int", column.columnDefinition() );
assertEquals( "table1", column.table() );
assertEquals( 50, column.length() );
assertEquals( 2, column.precision() );
assertEquals( 1, column.scale() );
}
@Test
public void testTemporal() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm18.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( Temporal.class );
assertAnnotationNotPresent( Enumerated.class );
assertAnnotationNotPresent( Lob.class );
assertEquals(
TemporalType.DATE, reader.getAnnotation(
Temporal.class
).value()
);
}
@Test
public void testEnumerated() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm19.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( Temporal.class );
assertAnnotationPresent( Enumerated.class );
assertAnnotationNotPresent( Lob.class );
assertEquals(
EnumType.STRING, reader.getAnnotation(
Enumerated.class
).value()
);
}
@Test
public void testLob() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm20.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( Temporal.class );
assertAnnotationNotPresent( Enumerated.class );
assertAnnotationPresent( Lob.class );
}
/**
* When there's a single attribute override, we still wrap it with an
* AttributeOverrides annotation.
*/
@Test
public void testSingleAttributeOverride() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm21.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 1, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "col1", overrides[0].column().name() );
}
@Test
public void testMultipleAttributeOverrides() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm22.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 2, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "", overrides[0].column().name() );
assertFalse( overrides[0].column().unique() );
assertTrue( overrides[0].column().nullable() );
assertTrue( overrides[0].column().insertable() );
assertTrue( overrides[0].column().updatable() );
assertEquals( "", overrides[0].column().columnDefinition() );
assertEquals( "", overrides[0].column().table() );
assertEquals( 255, overrides[0].column().length() );
assertEquals( 0, overrides[0].column().precision() );
assertEquals( 0, overrides[0].column().scale() );
assertEquals( "field2", overrides[1].name() );
assertEquals( "col1", overrides[1].column().name() );
assertTrue( overrides[1].column().unique() );
assertFalse( overrides[1].column().nullable() );
assertFalse( overrides[1].column().insertable() );
assertFalse( overrides[1].column().updatable() );
assertEquals( "int", overrides[1].column().columnDefinition() );
assertEquals( "table1", overrides[1].column().table() );
assertEquals( 50, overrides[1].column().length() );
assertEquals( 2, overrides[1].column().precision() );
assertEquals( 1, overrides[1].column().scale() );
}
/**
* Tests that map-key-attribute-override and attribute-override elements
* both end up in the AttributeOverrides annotation.
*/
@Test
public void testMixedAttributeOverrides() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm23.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 2, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "col1", overrides[0].column().name() );
assertEquals( "field2", overrides[1].name() );
assertEquals( "col2", overrides[1].column().name() );
}
/**
* When there's a single association override, we still wrap it with an
* AssociationOverrides annotation.
*/
@Test
public void testSingleAssociationOverride() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm24.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( AssociationOverride.class );
assertAnnotationPresent( AssociationOverrides.class );
AssociationOverrides overridesAnno = reader.getAnnotation( AssociationOverrides.class );
AssociationOverride[] overrides = overridesAnno.value();
assertEquals( 1, overrides.length );
assertEquals( "association1", overrides[0].name() );
assertEquals( 0, overrides[0].joinColumns().length );
assertEquals( "", overrides[0].joinTable().name() );
}
@Test
public void testMultipleAssociationOverridesJoinColumns() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm25.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( AssociationOverride.class );
assertAnnotationPresent( AssociationOverrides.class );
AssociationOverrides overridesAnno = reader.getAnnotation( AssociationOverrides.class );
AssociationOverride[] overrides = overridesAnno.value();
assertEquals( 2, overrides.length );
//First, an association using join table
assertEquals( "association1", overrides[0].name() );
assertEquals( 0, overrides[0].joinColumns().length );
JoinTable joinTableAnno = overrides[0].joinTable();
assertEquals( "catalog1", joinTableAnno.catalog() );
assertEquals( "table1", joinTableAnno.name() );
assertEquals( "schema1", joinTableAnno.schema() );
//JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table2", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
//InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals( 2, inverseJoinColumns.length );
assertEquals( "", inverseJoinColumns[0].name() );
assertEquals( "", inverseJoinColumns[0].referencedColumnName() );
assertEquals( "", inverseJoinColumns[0].table() );
assertEquals( "", inverseJoinColumns[0].columnDefinition() );
assertTrue( inverseJoinColumns[0].insertable() );
assertTrue( inverseJoinColumns[0].updatable() );
assertTrue( inverseJoinColumns[0].nullable() );
assertFalse( inverseJoinColumns[0].unique() );
assertEquals( "col3", inverseJoinColumns[1].name() );
assertEquals( "col4", inverseJoinColumns[1].referencedColumnName() );
assertEquals( "table3", inverseJoinColumns[1].table() );
assertEquals( "int", inverseJoinColumns[1].columnDefinition() );
assertFalse( inverseJoinColumns[1].insertable() );
assertFalse( inverseJoinColumns[1].updatable() );
assertFalse( inverseJoinColumns[1].nullable() );
assertTrue( inverseJoinColumns[1].unique() );
//UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno
.uniqueConstraints();
assertEquals( 2, uniqueConstraints.length );
assertEquals( "", uniqueConstraints[0].name() );
assertEquals( 1, uniqueConstraints[0].columnNames().length );
assertEquals( "col5", uniqueConstraints[0].columnNames()[0] );
assertEquals( "uq1", uniqueConstraints[1].name() );
assertEquals( 2, uniqueConstraints[1].columnNames().length );
assertEquals( "col6", uniqueConstraints[1].columnNames()[0] );
assertEquals( "col7", uniqueConstraints[1].columnNames()[1] );
//Second, an association using join columns
assertEquals( "association2", overrides[1].name() );
//JoinColumns
joinColumns = overrides[1].joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col8", joinColumns[1].name() );
assertEquals( "col9", joinColumns[1].referencedColumnName() );
assertEquals( "table4", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
}
@Test
public void testCollectionTableNoChildren() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm26.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( CollectionTable.class );
CollectionTable tableAnno = reader.getAnnotation( CollectionTable.class );
assertEquals( "", tableAnno.name() );
assertEquals( "", tableAnno.catalog() );
assertEquals( "", tableAnno.schema() );
assertEquals( 0, tableAnno.joinColumns().length );
assertEquals( 0, tableAnno.uniqueConstraints().length );
}
@Test
public void testCollectionTableAllChildren() throws Exception {
reader = getReader( Entity3.class, "field1", "element-collection.orm27.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationPresent( CollectionTable.class );
CollectionTable tableAnno = reader.getAnnotation( CollectionTable.class );
assertEquals( "table1", tableAnno.name() );
assertEquals( "catalog1", tableAnno.catalog() );
assertEquals( "schema1", tableAnno.schema() );
//JoinColumns
JoinColumn[] joinColumns = tableAnno.joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table2", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
//UniqueConstraints
UniqueConstraint[] uniqueConstraints = tableAnno.uniqueConstraints();
assertEquals( 2, uniqueConstraints.length );
assertEquals( "", uniqueConstraints[0].name() );
assertEquals( 1, uniqueConstraints[0].columnNames().length );
assertEquals( "col3", uniqueConstraints[0].columnNames()[0] );
assertEquals( "uq1", uniqueConstraints[1].name() );
assertEquals( 2, uniqueConstraints[1].columnNames().length );
assertEquals( "col4", uniqueConstraints[1].columnNames()[0] );
assertEquals( "col5", uniqueConstraints[1].columnNames()[1] );
}
@Test
public void testAllAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "element-collection.orm28.xml" );
assertAnnotationPresent( ElementCollection.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( Column.class );
assertAnnotationNotPresent( Temporal.class );
assertAnnotationNotPresent( Enumerated.class );
assertAnnotationNotPresent( Lob.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationNotPresent( AttributeOverrides.class );
assertAnnotationNotPresent( AssociationOverride.class );
assertAnnotationNotPresent( AssociationOverrides.class );
assertAnnotationNotPresent( CollectionTable.class );
assertAnnotationPresent( Access.class );
ElementCollection relAnno = reader.getAnnotation( ElementCollection.class );
assertEquals( FetchType.EAGER, relAnno.fetch() );
assertEquals( Entity3.class, relAnno.targetClass() );
assertEquals(
AccessType.PROPERTY, reader.getAnnotation( Access.class )
.value()
);
}
}

View File

@ -1,500 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.xml.ejb3;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.CascadeType;
import jakarta.persistence.EnumType;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.MapKey;
import jakarta.persistence.MapKeyClass;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.MapKeyEnumerated;
import jakarta.persistence.MapKeyJoinColumn;
import jakarta.persistence.MapKeyJoinColumns;
import jakarta.persistence.MapKeyTemporal;
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.TemporalType;
import jakarta.persistence.UniqueConstraint;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@TestForIssue(jiraKey = "HHH-14529")
public class Ejb3XmlManyToManyTest extends Ejb3XmlTestCase {
@Test
public void testNoChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm1.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationNotPresent( Access.class );
ManyToMany relAnno = reader.getAnnotation( ManyToMany.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.LAZY, relAnno.fetch() );
assertEquals( "", relAnno.mappedBy() );
assertEquals( void.class, relAnno.targetEntity() );
}
@Test
public void testOrderBy() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm2.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertEquals(
"col1 ASC, col2 DESC", reader.getAnnotation( OrderBy.class )
.value()
);
}
@Test
public void testOrderColumnNoAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm3.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationPresent( OrderColumn.class );
OrderColumn orderColumnAnno = reader.getAnnotation( OrderColumn.class );
assertEquals( "", orderColumnAnno.columnDefinition() );
assertEquals( "", orderColumnAnno.name() );
assertTrue( orderColumnAnno.insertable() );
assertTrue( orderColumnAnno.nullable() );
assertTrue( orderColumnAnno.updatable() );
}
@Test
public void testOrderColumnAllAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm4.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationPresent( OrderColumn.class );
OrderColumn orderColumnAnno = reader.getAnnotation( OrderColumn.class );
assertEquals( "int", orderColumnAnno.columnDefinition() );
assertEquals( "col1", orderColumnAnno.name() );
assertFalse( orderColumnAnno.insertable() );
assertFalse( orderColumnAnno.nullable() );
assertFalse( orderColumnAnno.updatable() );
}
@Test
public void testMapKeyNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm5.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals( "", reader.getAnnotation( MapKey.class ).name() );
}
@Test
public void testMapKeyAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm6.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals( "field2", reader.getAnnotation( MapKey.class ).name() );
}
@Test
public void testMapKeyClass() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm7.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
Entity2.class, reader.getAnnotation( MapKeyClass.class )
.value()
);
}
@Test
public void testMapKeyTemporal() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm8.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
TemporalType.DATE, reader.getAnnotation(
MapKeyTemporal.class
).value()
);
}
@Test
public void testMapKeyEnumerated() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm9.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
EnumType.STRING, reader.getAnnotation(
MapKeyEnumerated.class
).value()
);
}
/**
* When there's a single map key attribute override, we still wrap it with
* an AttributeOverrides annotation.
*/
@Test
public void testSingleMapKeyAttributeOverride() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm10.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 1, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "col1", overrides[0].column().name() );
}
@Test
public void testMultipleMapKeyAttributeOverrides() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm11.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 2, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "", overrides[0].column().name() );
assertFalse( overrides[0].column().unique() );
assertTrue( overrides[0].column().nullable() );
assertTrue( overrides[0].column().insertable() );
assertTrue( overrides[0].column().updatable() );
assertEquals( "", overrides[0].column().columnDefinition() );
assertEquals( "", overrides[0].column().table() );
assertEquals( 255, overrides[0].column().length() );
assertEquals( 0, overrides[0].column().precision() );
assertEquals( 0, overrides[0].column().scale() );
assertEquals( "field2", overrides[1].name() );
assertEquals( "col1", overrides[1].column().name() );
assertTrue( overrides[1].column().unique() );
assertFalse( overrides[1].column().nullable() );
assertFalse( overrides[1].column().insertable() );
assertFalse( overrides[1].column().updatable() );
assertEquals( "int", overrides[1].column().columnDefinition() );
assertEquals( "table1", overrides[1].column().table() );
assertEquals( 50, overrides[1].column().length() );
assertEquals( 2, overrides[1].column().precision() );
assertEquals( 1, overrides[1].column().scale() );
}
@Test
public void testMapKeyColumnNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm12.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyColumn keyColAnno = reader.getAnnotation( MapKeyColumn.class );
assertEquals( "", keyColAnno.columnDefinition() );
assertEquals( "", keyColAnno.name() );
assertEquals( "", keyColAnno.table() );
assertFalse( keyColAnno.nullable() );
assertTrue( keyColAnno.insertable() );
assertFalse( keyColAnno.unique() );
assertTrue( keyColAnno.updatable() );
assertEquals( 255, keyColAnno.length() );
assertEquals( 0, keyColAnno.precision() );
assertEquals( 0, keyColAnno.scale() );
}
@Test
public void testMapKeyColumnAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm13.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyColumn keyColAnno = reader.getAnnotation( MapKeyColumn.class );
assertEquals( "int", keyColAnno.columnDefinition() );
assertEquals( "col1", keyColAnno.name() );
assertEquals( "table1", keyColAnno.table() );
assertTrue( keyColAnno.nullable() );
assertFalse( keyColAnno.insertable() );
assertTrue( keyColAnno.unique() );
assertFalse( keyColAnno.updatable() );
assertEquals( 50, keyColAnno.length() );
assertEquals( 2, keyColAnno.precision() );
assertEquals( 1, keyColAnno.scale() );
}
/**
* When there's a single map key join column, we still wrap it with a
* MapKeyJoinColumns annotation.
*/
@Test
public void testSingleMapKeyJoinColumn() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm14.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyJoinColumns joinColumnsAnno = reader
.getAnnotation( MapKeyJoinColumns.class );
MapKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
}
@Test
public void testMultipleMapKeyJoinColumns() throws Exception {
reader = getReader( Entity3.class, "field1", "many-to-many.orm15.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyJoinColumns joinColumnsAnno = reader
.getAnnotation( MapKeyJoinColumns.class );
MapKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertFalse( joinColumns[0].unique() );
assertFalse( joinColumns[0].nullable() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertTrue( joinColumns[1].unique() );
assertTrue( joinColumns[1].nullable() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertEquals( "table1", joinColumns[1].table() );
}
@Test
public void testJoinTableNoChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm16.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "", joinTableAnno.catalog() );
assertEquals( "", joinTableAnno.name() );
assertEquals( "", joinTableAnno.schema() );
assertEquals( 0, joinTableAnno.joinColumns().length );
assertEquals( 0, joinTableAnno.inverseJoinColumns().length );
assertEquals( 0, joinTableAnno.uniqueConstraints().length );
}
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm17.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "cat1", joinTableAnno.catalog() );
assertEquals( "table1", joinTableAnno.name() );
assertEquals( "schema1", joinTableAnno.schema() );
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table2", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals( 2, inverseJoinColumns.length );
assertEquals( "", inverseJoinColumns[0].name() );
assertEquals( "", inverseJoinColumns[0].referencedColumnName() );
assertEquals( "", inverseJoinColumns[0].table() );
assertEquals( "", inverseJoinColumns[0].columnDefinition() );
assertTrue( inverseJoinColumns[0].insertable() );
assertTrue( inverseJoinColumns[0].updatable() );
assertTrue( inverseJoinColumns[0].nullable() );
assertFalse( inverseJoinColumns[0].unique() );
assertEquals( "col3", inverseJoinColumns[1].name() );
assertEquals( "col4", inverseJoinColumns[1].referencedColumnName() );
assertEquals( "table3", inverseJoinColumns[1].table() );
assertEquals( "int", inverseJoinColumns[1].columnDefinition() );
assertFalse( inverseJoinColumns[1].insertable() );
assertFalse( inverseJoinColumns[1].updatable() );
assertFalse( inverseJoinColumns[1].nullable() );
assertTrue( inverseJoinColumns[1].unique() );
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno
.uniqueConstraints();
assertEquals( 2, uniqueConstraints.length );
assertEquals( "", uniqueConstraints[0].name() );
assertEquals( 1, uniqueConstraints[0].columnNames().length );
assertEquals( "col5", uniqueConstraints[0].columnNames()[0] );
assertEquals( "uq1", uniqueConstraints[1].name() );
assertEquals( 2, uniqueConstraints[1].columnNames().length );
assertEquals( "col6", uniqueConstraints[1].columnNames()[0] );
assertEquals( "col7", uniqueConstraints[1].columnNames()[1] );
}
@Test
public void testCascadeAll() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm18.xml" );
assertAnnotationPresent( ManyToMany.class );
ManyToMany relAnno = reader.getAnnotation( ManyToMany.class );
assertEquals( 1, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
}
@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm19.xml" );
assertAnnotationPresent( ManyToMany.class );
ManyToMany relAnno = reader.getAnnotation( ManyToMany.class );
assertEquals( 4, relAnno.cascade().length );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[0] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[1] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[2] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[3] );
}
/**
* Make sure that it doesn't break the handler when {@link CascadeType#ALL}
* is specified in addition to a default cascade-persist or individual
* cascade settings.
*/
@Test
public void testCascadeAllPlusMore() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm20.xml" );
assertAnnotationPresent( ManyToMany.class );
ManyToMany relAnno = reader.getAnnotation( ManyToMany.class );
assertEquals( 6, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[1] );
assertEquals( CascadeType.MERGE, relAnno.cascade()[2] );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[3] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[4] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[5] );
}
@Test
public void testAllAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "many-to-many.orm21.xml" );
assertAnnotationPresent( ManyToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationPresent( Access.class );
ManyToMany relAnno = reader.getAnnotation( ManyToMany.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.EAGER, relAnno.fetch() );
assertEquals( "field2", relAnno.mappedBy() );
assertEquals( Entity3.class, relAnno.targetEntity() );
assertEquals(
AccessType.PROPERTY, reader.getAnnotation( Access.class )
.value()
);
}
}

View File

@ -1,237 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.xml.ejb3;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.CascadeType;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
import jakarta.persistence.UniqueConstraint;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@TestForIssue(jiraKey = "HHH-14529")
public class Ejb3XmlManyToOneTest extends Ejb3XmlTestCase {
@Test
public void testNoJoins() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm1.xml" );
assertAnnotationPresent( ManyToOne.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationNotPresent( Id.class );
assertAnnotationNotPresent( MapsId.class );
assertAnnotationNotPresent( Access.class );
ManyToOne relAnno = reader.getAnnotation( ManyToOne.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.EAGER, relAnno.fetch() );
assertTrue( relAnno.optional() );
assertEquals( void.class, relAnno.targetEntity() );
}
/**
* When there's a single join column, we still wrap it with a JoinColumns
* annotation.
*/
@Test
public void testSingleJoinColumn() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm2.xml" );
assertAnnotationPresent( ManyToOne.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
JoinColumns joinColumnsAnno = reader.getAnnotation( JoinColumns.class );
JoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
assertEquals( "col2", joinColumns[0].referencedColumnName() );
assertEquals( "table1", joinColumns[0].table() );
}
@Test
public void testMultipleJoinColumns() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm3.xml" );
assertAnnotationPresent( ManyToOne.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
JoinColumns joinColumnsAnno = reader.getAnnotation( JoinColumns.class );
JoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table1", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
}
@Test
public void testJoinTableNoChildren() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm4.xml" );
assertAnnotationPresent( ManyToOne.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationPresent( JoinTable.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "", joinTableAnno.catalog() );
assertEquals( "", joinTableAnno.name() );
assertEquals( "", joinTableAnno.schema() );
assertEquals( 0, joinTableAnno.joinColumns().length );
assertEquals( 0, joinTableAnno.inverseJoinColumns().length );
assertEquals( 0, joinTableAnno.uniqueConstraints().length );
}
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm5.xml" );
assertAnnotationPresent( ManyToOne.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationPresent( JoinTable.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "cat1", joinTableAnno.catalog() );
assertEquals( "table1", joinTableAnno.name() );
assertEquals( "schema1", joinTableAnno.schema() );
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table2", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals( 2, inverseJoinColumns.length );
assertEquals( "", inverseJoinColumns[0].name() );
assertEquals( "", inverseJoinColumns[0].referencedColumnName() );
assertEquals( "", inverseJoinColumns[0].table() );
assertEquals( "", inverseJoinColumns[0].columnDefinition() );
assertTrue( inverseJoinColumns[0].insertable() );
assertTrue( inverseJoinColumns[0].updatable() );
assertTrue( inverseJoinColumns[0].nullable() );
assertFalse( inverseJoinColumns[0].unique() );
assertEquals( "col3", inverseJoinColumns[1].name() );
assertEquals( "col4", inverseJoinColumns[1].referencedColumnName() );
assertEquals( "table3", inverseJoinColumns[1].table() );
assertEquals( "int", inverseJoinColumns[1].columnDefinition() );
assertFalse( inverseJoinColumns[1].insertable() );
assertFalse( inverseJoinColumns[1].updatable() );
assertFalse( inverseJoinColumns[1].nullable() );
assertTrue( inverseJoinColumns[1].unique() );
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno
.uniqueConstraints();
assertEquals( 2, uniqueConstraints.length );
assertEquals( "", uniqueConstraints[0].name() );
assertEquals( 1, uniqueConstraints[0].columnNames().length );
assertEquals( "col5", uniqueConstraints[0].columnNames()[0] );
assertEquals( "uq1", uniqueConstraints[1].name() );
assertEquals( 2, uniqueConstraints[1].columnNames().length );
assertEquals( "col6", uniqueConstraints[1].columnNames()[0] );
assertEquals( "col7", uniqueConstraints[1].columnNames()[1] );
}
@Test
public void testAllAttributes() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm6.xml" );
assertAnnotationPresent( ManyToOne.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationPresent( Id.class );
assertAnnotationPresent( MapsId.class );
assertAnnotationPresent( Access.class );
ManyToOne relAnno = reader.getAnnotation( ManyToOne.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.LAZY, relAnno.fetch() );
assertFalse( relAnno.optional() );
assertEquals( Entity3.class, relAnno.targetEntity() );
assertEquals( "col1", reader.getAnnotation( MapsId.class ).value() );
assertEquals(
AccessType.PROPERTY, reader.getAnnotation( Access.class )
.value()
);
}
@Test
public void testCascadeAll() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm7.xml" );
assertAnnotationPresent( ManyToOne.class );
ManyToOne relAnno = reader.getAnnotation( ManyToOne.class );
assertEquals( 1, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
}
@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm8.xml" );
assertAnnotationPresent( ManyToOne.class );
ManyToOne relAnno = reader.getAnnotation( ManyToOne.class );
assertEquals( 4, relAnno.cascade().length );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[0] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[1] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[2] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[3] );
}
/**
* Make sure that it doesn't break the handler when {@link CascadeType#ALL}
* is specified in addition to a default cascade-persist or individual
* cascade settings.
*/
@Test
public void testCascadeAllPlusMore() throws Exception {
reader = getReader( Entity1.class, "field1", "many-to-one.orm9.xml" );
assertAnnotationPresent( ManyToOne.class );
ManyToOne relAnno = reader.getAnnotation( ManyToOne.class );
assertEquals( 6, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[1] );
assertEquals( CascadeType.MERGE, relAnno.cascade()[2] );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[3] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[4] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[5] );
}
}

View File

@ -1,553 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.xml.ejb3;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.CascadeType;
import jakarta.persistence.EnumType;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.MapKey;
import jakarta.persistence.MapKeyClass;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.MapKeyEnumerated;
import jakarta.persistence.MapKeyJoinColumn;
import jakarta.persistence.MapKeyJoinColumns;
import jakarta.persistence.MapKeyTemporal;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.TemporalType;
import jakarta.persistence.UniqueConstraint;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@TestForIssue(jiraKey = "HHH-14529")
public class Ejb3XmlOneToManyTest extends Ejb3XmlTestCase {
@Test
public void testNoChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm1.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( Access.class );
OneToMany relAnno = reader.getAnnotation( OneToMany.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.LAZY, relAnno.fetch() );
assertEquals( "", relAnno.mappedBy() );
assertFalse( relAnno.orphanRemoval() );
assertEquals( void.class, relAnno.targetEntity() );
}
@Test
public void testOrderBy() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm2.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertEquals(
"col1 ASC, col2 DESC", reader.getAnnotation( OrderBy.class )
.value()
);
}
@Test
public void testOrderColumnNoAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm3.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationPresent( OrderColumn.class );
OrderColumn orderColumnAnno = reader.getAnnotation( OrderColumn.class );
assertEquals( "", orderColumnAnno.columnDefinition() );
assertEquals( "", orderColumnAnno.name() );
assertTrue( orderColumnAnno.insertable() );
assertTrue( orderColumnAnno.nullable() );
assertTrue( orderColumnAnno.updatable() );
}
@Test
public void testOrderColumnAllAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm4.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationPresent( OrderColumn.class );
OrderColumn orderColumnAnno = reader.getAnnotation( OrderColumn.class );
assertEquals( "int", orderColumnAnno.columnDefinition() );
assertEquals( "col1", orderColumnAnno.name() );
assertFalse( orderColumnAnno.insertable() );
assertFalse( orderColumnAnno.nullable() );
assertFalse( orderColumnAnno.updatable() );
}
@Test
public void testMapKeyNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm5.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals( "", reader.getAnnotation( MapKey.class ).name() );
}
@Test
public void testMapKeyAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm6.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals( "field2", reader.getAnnotation( MapKey.class ).name() );
}
@Test
public void testMapKeyClass() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm7.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
Entity2.class, reader.getAnnotation( MapKeyClass.class )
.value()
);
}
@Test
public void testMapKeyTemporal() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm8.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
TemporalType.DATE, reader.getAnnotation(
MapKeyTemporal.class
).value()
);
}
@Test
public void testMapKeyEnumerated() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm9.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertEquals(
EnumType.STRING, reader.getAnnotation(
MapKeyEnumerated.class
).value()
);
}
/**
* When there's a single map key attribute override, we still wrap it with
* an AttributeOverrides annotation.
*/
@Test
public void testSingleMapKeyAttributeOverride() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm10.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 1, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "col1", overrides[0].column().name() );
}
@Test
public void testMultipleMapKeyAttributeOverrides() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm11.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( AttributeOverride.class );
assertAnnotationPresent( AttributeOverrides.class );
AttributeOverrides overridesAnno = reader
.getAnnotation( AttributeOverrides.class );
AttributeOverride[] overrides = overridesAnno.value();
assertEquals( 2, overrides.length );
assertEquals( "field1", overrides[0].name() );
assertEquals( "", overrides[0].column().name() );
assertFalse( overrides[0].column().unique() );
assertTrue( overrides[0].column().nullable() );
assertTrue( overrides[0].column().insertable() );
assertTrue( overrides[0].column().updatable() );
assertEquals( "", overrides[0].column().columnDefinition() );
assertEquals( "", overrides[0].column().table() );
assertEquals( 255, overrides[0].column().length() );
assertEquals( 0, overrides[0].column().precision() );
assertEquals( 0, overrides[0].column().scale() );
assertEquals( "field2", overrides[1].name() );
assertEquals( "col1", overrides[1].column().name() );
assertTrue( overrides[1].column().unique() );
assertFalse( overrides[1].column().nullable() );
assertFalse( overrides[1].column().insertable() );
assertFalse( overrides[1].column().updatable() );
assertEquals( "int", overrides[1].column().columnDefinition() );
assertEquals( "table1", overrides[1].column().table() );
assertEquals( 50, overrides[1].column().length() );
assertEquals( 2, overrides[1].column().precision() );
assertEquals( 1, overrides[1].column().scale() );
}
@Test
public void testMapKeyColumnNoAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm12.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyColumn keyColAnno = reader.getAnnotation( MapKeyColumn.class );
assertEquals( "", keyColAnno.columnDefinition() );
assertEquals( "", keyColAnno.name() );
assertEquals( "", keyColAnno.table() );
assertFalse( keyColAnno.nullable() );
assertTrue( keyColAnno.insertable() );
assertFalse( keyColAnno.unique() );
assertTrue( keyColAnno.updatable() );
assertEquals( 255, keyColAnno.length() );
assertEquals( 0, keyColAnno.precision() );
assertEquals( 0, keyColAnno.scale() );
}
@Test
public void testMapKeyColumnAllAttributes() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm13.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyColumn keyColAnno = reader.getAnnotation( MapKeyColumn.class );
assertEquals( "int", keyColAnno.columnDefinition() );
assertEquals( "col1", keyColAnno.name() );
assertEquals( "table1", keyColAnno.table() );
assertTrue( keyColAnno.nullable() );
assertFalse( keyColAnno.insertable() );
assertTrue( keyColAnno.unique() );
assertFalse( keyColAnno.updatable() );
assertEquals( 50, keyColAnno.length() );
assertEquals( 2, keyColAnno.precision() );
assertEquals( 1, keyColAnno.scale() );
}
/**
* When there's a single map key join column, we still wrap it with a
* MapKeyJoinColumns annotation.
*/
@Test
public void testSingleMapKeyJoinColumn() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm14.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyJoinColumns joinColumnsAnno = reader
.getAnnotation( MapKeyJoinColumns.class );
MapKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
}
@Test
public void testMultipleMapKeyJoinColumns() throws Exception {
reader = getReader( Entity3.class, "field1", "one-to-many.orm15.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
MapKeyJoinColumns joinColumnsAnno = reader
.getAnnotation( MapKeyJoinColumns.class );
MapKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertFalse( joinColumns[0].unique() );
assertFalse( joinColumns[0].nullable() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertTrue( joinColumns[1].unique() );
assertTrue( joinColumns[1].nullable() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertEquals( "table1", joinColumns[1].table() );
}
@Test
public void testJoinTableNoChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm16.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "", joinTableAnno.catalog() );
assertEquals( "", joinTableAnno.name() );
assertEquals( "", joinTableAnno.schema() );
assertEquals( 0, joinTableAnno.joinColumns().length );
assertEquals( 0, joinTableAnno.inverseJoinColumns().length );
assertEquals( 0, joinTableAnno.uniqueConstraints().length );
}
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm17.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "cat1", joinTableAnno.catalog() );
assertEquals( "table1", joinTableAnno.name() );
assertEquals( "schema1", joinTableAnno.schema() );
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table2", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals( 2, inverseJoinColumns.length );
assertEquals( "", inverseJoinColumns[0].name() );
assertEquals( "", inverseJoinColumns[0].referencedColumnName() );
assertEquals( "", inverseJoinColumns[0].table() );
assertEquals( "", inverseJoinColumns[0].columnDefinition() );
assertTrue( inverseJoinColumns[0].insertable() );
assertTrue( inverseJoinColumns[0].updatable() );
assertTrue( inverseJoinColumns[0].nullable() );
assertFalse( inverseJoinColumns[0].unique() );
assertEquals( "col3", inverseJoinColumns[1].name() );
assertEquals( "col4", inverseJoinColumns[1].referencedColumnName() );
assertEquals( "table3", inverseJoinColumns[1].table() );
assertEquals( "int", inverseJoinColumns[1].columnDefinition() );
assertFalse( inverseJoinColumns[1].insertable() );
assertFalse( inverseJoinColumns[1].updatable() );
assertFalse( inverseJoinColumns[1].nullable() );
assertTrue( inverseJoinColumns[1].unique() );
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno
.uniqueConstraints();
assertEquals( 2, uniqueConstraints.length );
assertEquals( "", uniqueConstraints[0].name() );
assertEquals( 1, uniqueConstraints[0].columnNames().length );
assertEquals( "col5", uniqueConstraints[0].columnNames()[0] );
assertEquals( "uq1", uniqueConstraints[1].name() );
assertEquals( 2, uniqueConstraints[1].columnNames().length );
assertEquals( "col6", uniqueConstraints[1].columnNames()[0] );
assertEquals( "col7", uniqueConstraints[1].columnNames()[1] );
}
/**
* When there's a single join column, we still wrap it with a JoinColumns
* annotation.
*/
@Test
public void testSingleJoinColumn() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm18.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
JoinColumns joinColumnsAnno = reader.getAnnotation( JoinColumns.class );
JoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
assertEquals( "col2", joinColumns[0].referencedColumnName() );
assertEquals( "table1", joinColumns[0].table() );
}
@Test
public void testMultipleJoinColumns() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm19.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
JoinColumns joinColumnsAnno = reader.getAnnotation( JoinColumns.class );
JoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table1", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
}
@Test
public void testCascadeAll() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm20.xml" );
assertAnnotationPresent( OneToMany.class );
OneToMany relAnno = reader.getAnnotation( OneToMany.class );
assertEquals( 1, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
}
@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm21.xml" );
assertAnnotationPresent( OneToMany.class );
OneToMany relAnno = reader.getAnnotation( OneToMany.class );
assertEquals( 4, relAnno.cascade().length );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[0] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[1] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[2] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[3] );
}
/**
* Make sure that it doesn't break the handler when {@link CascadeType#ALL}
* is specified in addition to a default cascade-persist or individual
* cascade settings.
*/
@Test
public void testCascadeAllPlusMore() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm22.xml" );
assertAnnotationPresent( OneToMany.class );
OneToMany relAnno = reader.getAnnotation( OneToMany.class );
assertEquals( 6, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[1] );
assertEquals( CascadeType.MERGE, relAnno.cascade()[2] );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[3] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[4] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[5] );
}
@Test
public void testAllAttributes() throws Exception {
reader = getReader( Entity2.class, "field1", "one-to-many.orm23.xml" );
assertAnnotationPresent( OneToMany.class );
assertAnnotationNotPresent( OrderBy.class );
assertAnnotationNotPresent( OrderColumn.class );
assertAnnotationNotPresent( MapKey.class );
assertAnnotationNotPresent( MapKeyClass.class );
assertAnnotationNotPresent( MapKeyTemporal.class );
assertAnnotationNotPresent( MapKeyEnumerated.class );
assertAnnotationNotPresent( MapKeyColumn.class );
assertAnnotationNotPresent( MapKeyJoinColumns.class );
assertAnnotationNotPresent( MapKeyJoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationPresent( Access.class );
OneToMany relAnno = reader.getAnnotation( OneToMany.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.EAGER, relAnno.fetch() );
assertEquals( "field2", relAnno.mappedBy() );
assertTrue( relAnno.orphanRemoval() );
assertEquals( Entity3.class, relAnno.targetEntity() );
assertEquals(
AccessType.PROPERTY, reader.getAnnotation( Access.class )
.value()
);
}
}

View File

@ -1,301 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.annotations.xml.ejb3;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.CascadeType;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.JoinTable;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.PrimaryKeyJoinColumns;
import jakarta.persistence.UniqueConstraint;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@TestForIssue(jiraKey = "HHH-14529")
public class Ejb3XmlOneToOneTest extends Ejb3XmlTestCase {
@Test
public void testNoChildren() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm1.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( MapsId.class );
assertAnnotationNotPresent( Id.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumns.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationNotPresent( Access.class );
OneToOne relAnno = reader.getAnnotation( OneToOne.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.EAGER, relAnno.fetch() );
assertEquals( "", relAnno.mappedBy() );
assertTrue( relAnno.optional() );
assertFalse( relAnno.orphanRemoval() );
assertEquals( void.class, relAnno.targetEntity() );
}
/**
* When there's a single primary key join column, we still wrap it with
* a PrimaryKeyJoinColumns annotation.
*/
@Test
public void testSinglePrimaryKeyJoinColumn() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm2.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationPresent( PrimaryKeyJoinColumns.class );
PrimaryKeyJoinColumns joinColumnsAnno =
reader.getAnnotation( PrimaryKeyJoinColumns.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
PrimaryKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
assertEquals( "col2", joinColumns[0].referencedColumnName() );
assertEquals( "int", joinColumns[0].columnDefinition() );
}
@Test
public void testMultiplePrimaryKeyJoinColumn() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm3.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationPresent( PrimaryKeyJoinColumns.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
PrimaryKeyJoinColumns joinColumnsAnno =
reader.getAnnotation( PrimaryKeyJoinColumns.class );
PrimaryKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "int", joinColumns[1].columnDefinition() );
}
/**
* When there's a single join column, we still wrap it with a JoinColumns
* annotation.
*/
@Test
public void testSingleJoinColumn() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm4.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumns.class );
assertAnnotationPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
JoinColumns joinColumnsAnno = reader.getAnnotation( JoinColumns.class );
JoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 1, joinColumns.length );
assertEquals( "col1", joinColumns[0].name() );
assertEquals( "col2", joinColumns[0].referencedColumnName() );
assertEquals( "table1", joinColumns[0].table() );
}
@Test
public void testMultipleJoinColumns() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm5.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinTable.class );
JoinColumns joinColumnsAnno = reader.getAnnotation( JoinColumns.class );
JoinColumn[] joinColumns = joinColumnsAnno.value();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table1", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
}
@Test
public void testJoinTableNoChildren() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm6.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumns.class );
assertAnnotationPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "", joinTableAnno.catalog() );
assertEquals( "", joinTableAnno.name() );
assertEquals( "", joinTableAnno.schema() );
assertEquals( 0, joinTableAnno.joinColumns().length );
assertEquals( 0, joinTableAnno.inverseJoinColumns().length );
assertEquals( 0, joinTableAnno.uniqueConstraints().length );
}
@Test
public void testJoinTableAllChildren() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm7.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumns.class );
assertAnnotationPresent( JoinTable.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
JoinTable joinTableAnno = reader.getAnnotation( JoinTable.class );
assertEquals( "cat1", joinTableAnno.catalog() );
assertEquals( "table1", joinTableAnno.name() );
assertEquals( "schema1", joinTableAnno.schema() );
// JoinColumns
JoinColumn[] joinColumns = joinTableAnno.joinColumns();
assertEquals( 2, joinColumns.length );
assertEquals( "", joinColumns[0].name() );
assertEquals( "", joinColumns[0].referencedColumnName() );
assertEquals( "", joinColumns[0].table() );
assertEquals( "", joinColumns[0].columnDefinition() );
assertTrue( joinColumns[0].insertable() );
assertTrue( joinColumns[0].updatable() );
assertTrue( joinColumns[0].nullable() );
assertFalse( joinColumns[0].unique() );
assertEquals( "col1", joinColumns[1].name() );
assertEquals( "col2", joinColumns[1].referencedColumnName() );
assertEquals( "table2", joinColumns[1].table() );
assertEquals( "int", joinColumns[1].columnDefinition() );
assertFalse( joinColumns[1].insertable() );
assertFalse( joinColumns[1].updatable() );
assertFalse( joinColumns[1].nullable() );
assertTrue( joinColumns[1].unique() );
// InverseJoinColumns
JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
assertEquals( 2, inverseJoinColumns.length );
assertEquals( "", inverseJoinColumns[0].name() );
assertEquals( "", inverseJoinColumns[0].referencedColumnName() );
assertEquals( "", inverseJoinColumns[0].table() );
assertEquals( "", inverseJoinColumns[0].columnDefinition() );
assertTrue( inverseJoinColumns[0].insertable() );
assertTrue( inverseJoinColumns[0].updatable() );
assertTrue( inverseJoinColumns[0].nullable() );
assertFalse( inverseJoinColumns[0].unique() );
assertEquals( "col3", inverseJoinColumns[1].name() );
assertEquals( "col4", inverseJoinColumns[1].referencedColumnName() );
assertEquals( "table3", inverseJoinColumns[1].table() );
assertEquals( "int", inverseJoinColumns[1].columnDefinition() );
assertFalse( inverseJoinColumns[1].insertable() );
assertFalse( inverseJoinColumns[1].updatable() );
assertFalse( inverseJoinColumns[1].nullable() );
assertTrue( inverseJoinColumns[1].unique() );
// UniqueConstraints
UniqueConstraint[] uniqueConstraints = joinTableAnno
.uniqueConstraints();
assertEquals( 2, uniqueConstraints.length );
assertEquals( "", uniqueConstraints[0].name() );
assertEquals( 1, uniqueConstraints[0].columnNames().length );
assertEquals( "col5", uniqueConstraints[0].columnNames()[0] );
assertEquals( "uq1", uniqueConstraints[1].name() );
assertEquals( 2, uniqueConstraints[1].columnNames().length );
assertEquals( "col6", uniqueConstraints[1].columnNames()[0] );
assertEquals( "col7", uniqueConstraints[1].columnNames()[1] );
}
@Test
public void testCascadeAll() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm8.xml" );
assertAnnotationPresent( OneToOne.class );
OneToOne relAnno = reader.getAnnotation( OneToOne.class );
assertEquals( 1, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
}
@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm9.xml" );
assertAnnotationPresent( OneToOne.class );
OneToOne relAnno = reader.getAnnotation( OneToOne.class );
assertEquals( 4, relAnno.cascade().length );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[0] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[1] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[2] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[3] );
}
/**
* Make sure that it doesn't break the handler when {@link CascadeType#ALL}
* is specified in addition to a default cascade-persist or individual
* cascade settings.
*/
@Test
public void testCascadeAllPlusMore() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm10.xml" );
assertAnnotationPresent( OneToOne.class );
OneToOne relAnno = reader.getAnnotation( OneToOne.class );
assertEquals( 6, relAnno.cascade().length );
assertEquals( CascadeType.ALL, relAnno.cascade()[0] );
assertEquals( CascadeType.PERSIST, relAnno.cascade()[1] );
assertEquals( CascadeType.MERGE, relAnno.cascade()[2] );
assertEquals( CascadeType.REMOVE, relAnno.cascade()[3] );
assertEquals( CascadeType.REFRESH, relAnno.cascade()[4] );
assertEquals( CascadeType.DETACH, relAnno.cascade()[5] );
}
@Test
public void testAllAttributes() throws Exception {
reader = getReader( Entity1.class, "field1", "one-to-one.orm11.xml" );
assertAnnotationPresent( OneToOne.class );
assertAnnotationPresent( MapsId.class );
assertAnnotationPresent( Id.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumn.class );
assertAnnotationNotPresent( PrimaryKeyJoinColumns.class );
assertAnnotationNotPresent( JoinColumns.class );
assertAnnotationNotPresent( JoinColumn.class );
assertAnnotationNotPresent( JoinTable.class );
assertAnnotationPresent( Access.class );
OneToOne relAnno = reader.getAnnotation( OneToOne.class );
assertEquals( 0, relAnno.cascade().length );
assertEquals( FetchType.LAZY, relAnno.fetch() );
assertEquals( "field2", relAnno.mappedBy() );
assertFalse( relAnno.optional() );
assertTrue( relAnno.orphanRemoval() );
assertEquals( Entity3.class, relAnno.targetEntity() );
assertEquals(
AccessType.PROPERTY, reader.getAnnotation( Access.class )
.value()
);
assertEquals(
"field3", reader.getAnnotation( MapsId.class )
.value()
);
}
}

View File

@ -1,89 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.annotations.xml.ejb3;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.model.internal.JPAXMLOverriddenAnnotationReader;
import org.hibernate.boot.model.internal.XMLContext;
import org.hibernate.orm.test.internal.util.xml.XMLMappingHelper;
import org.hibernate.testing.boot.BootstrapContextImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Test superclass to provide utility methods for testing the mapping of JPA
* XML to JPA annotations. The configuration is built within each test, and no
* database is used. Thus, no schema generation or cleanup will be performed.
*/
public abstract class Ejb3XmlTestCase extends BaseUnitTestCase {
protected JPAXMLOverriddenAnnotationReader reader;
private BootstrapContextImpl bootstrapContext;
protected Ejb3XmlTestCase() {
}
@Before
public void init() {
bootstrapContext = new BootstrapContextImpl();
}
@After
public void destroy() {
bootstrapContext.close();
}
protected void assertAnnotationPresent(Class<? extends Annotation> annotationType) {
assertTrue(
"Expected annotation " + annotationType.getSimpleName() + " was not present",
reader.isAnnotationPresent( annotationType )
);
}
protected void assertAnnotationNotPresent(Class<? extends Annotation> annotationType) {
assertFalse(
"Unexpected annotation " + annotationType.getSimpleName() + " was present",
reader.isAnnotationPresent( annotationType )
);
}
protected JPAXMLOverriddenAnnotationReader getReader(Class<?> entityClass, String fieldName, String ormResourceName)
throws Exception {
AnnotatedElement el = getAnnotatedElement( entityClass, fieldName );
XMLContext xmlContext = getContext( ormResourceName );
return new JPAXMLOverriddenAnnotationReader( el, xmlContext, bootstrapContext );
}
protected AnnotatedElement getAnnotatedElement(Class<?> entityClass, String fieldName) throws Exception {
return entityClass.getDeclaredField( fieldName );
}
protected XMLContext getContext(String resourceName) throws Exception {
InputStream is = getClass().getResourceAsStream( resourceName );
assertNotNull( "Could not load resource " + resourceName, is );
return getContext( is, resourceName );
}
protected XMLContext getContext(InputStream is, String resourceName) throws Exception {
XMLMappingHelper xmlHelper = new XMLMappingHelper();
JaxbEntityMappingsImpl mappings = xmlHelper.readOrmXmlMappings( is, resourceName );
XMLContext context = new XMLContext( bootstrapContext );
context.addDocument( mappings );
return context;
}
}

View File

@ -12,7 +12,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.archive.scan.internal.StandardScanOptions;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
@ -188,11 +187,6 @@ public class BootstrapContextTesting implements BootstrapContext {
return scannerSetting;
}
@Override
public ReflectionManager getReflectionManager() {
return null;
}
@Override
public IndexView getJandexView() {
return jandexIndex;

View File

@ -15,7 +15,7 @@ dependencies {
implementation jakartaLibs.jaxbApi
implementation jakartaLibs.jaxb
implementation libs.hcann
implementation libs.hibernateModels
implementation libs.jandex
compileOnly libs.ant

View File

@ -35,7 +35,7 @@ dependencies {
api project( ":hibernate-enhance-maven-plugin" )
api project( ":hibernate-ant" )
api libs.hcann
api libs.hibernateModels
api jakartaLibs.jpa
api jakartaLibs.jta

View File

@ -40,7 +40,7 @@ dependencies {
api testLibs.log4j2
implementation libs.hcann
implementation libs.hibernateModels
implementation libs.jandex
implementation testLibs.wildFlyTxnClient
implementation testLibs.junit5Engine

View File

@ -9,7 +9,6 @@ package org.hibernate.testing.boot;
import java.util.Collection;
import java.util.Map;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
@ -123,11 +122,6 @@ public class BootstrapContextImpl implements BootstrapContext {
return delegate.getScanner();
}
@Override
public ReflectionManager getReflectionManager() {
return delegate.getReflectionManager();
}
@Override
public IndexView getJandexView() {
return delegate.getJandexView();

View File

@ -70,7 +70,7 @@ dependencyResolutionManagement {
def byteBuddyVersion = version "byteBuddy", "1.14.18"
def classmateVersion = version "classmate", "1.5.1"
def geolatteVersion = version "geolatte", "1.9.1"
def hibernateModelsVersion = version "hibernateModels", "0.7.6"
def hibernateModelsVersion = version "hibernateModels", "0.7.8"
def jandexVersion = version "jandex", "3.2.0"
def hcannVersion = version "hcann", "7.0.1.Final"
def jacksonVersion = version "jackson", "2.17.0"
@ -104,7 +104,6 @@ dependencyResolutionManagement {
library( "hibernateModels", "org.hibernate.models", "hibernate-models" ).versionRef( hibernateModelsVersion )
library( "jandex", "io.smallrye", "jandex" ).versionRef( jandexVersion )
library( "classmate", "com.fasterxml", "classmate" ).versionRef( classmateVersion )
library( "hcann", "org.hibernate.common", "hibernate-commons-annotations" ).versionRef( hcannVersion )
library( "jackson", "com.fasterxml.jackson.core", "jackson-databind" ).versionRef( jacksonVersion )
library( "jacksonXml", "com.fasterxml.jackson.dataformat", "jackson-dataformat-xml" ).versionRef( jacksonVersion )

View File

@ -53,7 +53,8 @@ task processPluginXml(type: Copy) {
generateMavenDependency(jakartaLibs.jpa)\
+ generateMavenDependency(libs.antlr)\
+ generateMavenDependency(jakartaLibs.jta)\
+ generateMavenDependency(libs.hcann)\
+ generateMavenDependency(libs.hibernateModels)\
+ generateMavenDependency(libs.jandex)\
+ generateMavenDependency(libs.byteBuddy)\
+ generateMavenDependency(libs.logging)\
+ generateMavenDependency("org.hibernate:hibernate-core:" + project.version)])

View File

@ -50,7 +50,7 @@ sourceSets {
dependencies {
// api - ewww... but Maven needs them this way
api project( ':hibernate-core' )
api libs.hcann
api libs.hibernateModels
api libs.jandex
api jakartaLibs.jaxbApi
api jakartaLibs.jaxb