Search in sources :

Example 1 with NotFoundAction

use of org.hibernate.annotations.NotFoundAction in project hibernate-orm by hibernate.

the class AnnotationBinder method bindManyToOne.

private static void bindManyToOne(PropertyHolder propertyHolder, PropertyData inferredData, boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext context, XProperty property, AnnotatedJoinColumn[] joinColumns, PropertyBinder propertyBinder, boolean forcePersist) {
    ManyToOne ann = property.getAnnotation(ManyToOne.class);
    // check validity
    if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
        throw new AnnotationException("@Column(s) not allowed on a @ManyToOne property: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    Cascade hibernateCascade = property.getAnnotation(Cascade.class);
    NotFound notFound = property.getAnnotation(NotFound.class);
    NotFoundAction notFoundAction = notFound == null ? null : notFound.action();
    matchIgnoreNotFoundWithFetchType(propertyHolder.getEntityName(), property.getName(), notFoundAction, ann.fetch());
    OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
    JoinTable assocTable = propertyHolder.getJoinTable(property);
    if (assocTable != null) {
        Join join = propertyHolder.addJoin(assocTable, false);
        for (AnnotatedJoinColumn joinColumn : joinColumns) {
            joinColumn.setExplicitTableName(join.getTable().getName());
        }
    }
    // MapsId means the columns belong to the pk;
    // A @MapsId association (obviously) must be non-null when the entity is first persisted.
    // If a @MapsId association is not mapped with @NotFound(IGNORE), then the association
    // is mandatory (even if the association has optional=true).
    // If a @MapsId association has optional=true and is mapped with @NotFound(IGNORE) then
    // the association is optional.
    final boolean mandatory = !ann.optional() || property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(MapsId.class) && notFoundAction != null;
    bindManyToOne(getCascadeStrategy(ann.cascade(), hibernateCascade, false, forcePersist), joinColumns, !mandatory, notFoundAction, onDeleteAnn != null && OnDeleteAction.CASCADE == onDeleteAnn.action(), ToOneBinder.getTargetEntity(inferredData, context), propertyHolder, inferredData, false, isIdentifierMapper, inSecondPass, propertyBinder, context);
}
Also used : MapKeyColumn(jakarta.persistence.MapKeyColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) Column(jakarta.persistence.Column) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) OrderColumn(jakarta.persistence.OrderColumn) DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) AnnotatedDiscriminatorColumn.buildDiscriminatorColumn(org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn) AnnotatedJoinColumn.buildJoinTableJoinColumns(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns) JoinColumns(jakarta.persistence.JoinColumns) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) AnnotationException(org.hibernate.AnnotationException) Join(org.hibernate.mapping.Join) NotFoundAction(org.hibernate.annotations.NotFoundAction) ManyToOne(jakarta.persistence.ManyToOne) Cascade(org.hibernate.annotations.Cascade) NotFound(org.hibernate.annotations.NotFound) OnDelete(org.hibernate.annotations.OnDelete) JoinTable(jakarta.persistence.JoinTable)

Example 2 with NotFoundAction

use of org.hibernate.annotations.NotFoundAction in project hibernate-orm by hibernate.

the class CollectionBinderTest method testAssociatedClassException.

@Test
@TestForIssue(jiraKey = "HHH-10106")
public void testAssociatedClassException() throws SQLException {
    final Collection collection = mock(Collection.class);
    final XClass collectionType = mock(XClass.class);
    final MetadataBuildingContext buildingContext = mock(MetadataBuildingContext.class);
    final InFlightMetadataCollector inFly = mock(InFlightMetadataCollector.class);
    final PersistentClass persistentClass = mock(PersistentClass.class);
    final Table table = mock(Table.class);
    when(buildingContext.getMetadataCollector()).thenReturn(inFly);
    when(collection.getOwner()).thenReturn(persistentClass);
    when(collectionType.getName()).thenReturn("List");
    when(persistentClass.getTable()).thenReturn(table);
    when(table.getName()).thenReturn("Hibernate");
    String expectMessage = "Association [abc] for entity [CollectionBinderTest] references unmapped class [List]";
    try {
        new CollectionBinder(null, false, buildingContext) {

            {
                final PropertyHolder propertyHolder = Mockito.mock(PropertyHolder.class);
                when(propertyHolder.getClassName()).thenReturn(CollectionBinderTest.class.getSimpleName());
                this.propertyName = "abc";
                setPropertyHolder(propertyHolder);
            }

            @Override
            protected Collection createCollection(PersistentClass persistentClass) {
                return null;
            }

            @Override
            public void bindOneToManySecondPass(Collection collection, Map<String, PersistentClass> persistentClasses, AnnotatedJoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, NotFoundAction notFoundAction, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
                super.bindOneToManySecondPass(collection, persistentClasses, fkJoinColumns, collectionType, cascadeDeleteEnabled, notFoundAction, buildingContext, inheritanceStatePerClass);
            }
        }.bindOneToManySecondPass(collection, new HashMap(), null, collectionType, false, null, buildingContext, null);
    } catch (MappingException e) {
        assertEquals(expectMessage, e.getMessage());
    }
}
Also used : Table(org.hibernate.mapping.Table) HashMap(java.util.HashMap) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) NotFoundAction(org.hibernate.annotations.NotFoundAction) XClass(org.hibernate.annotations.common.reflection.XClass) MappingException(org.hibernate.MappingException) InheritanceState(org.hibernate.cfg.InheritanceState) InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) PropertyHolder(org.hibernate.cfg.PropertyHolder) Collection(org.hibernate.mapping.Collection) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) CollectionBinder(org.hibernate.cfg.annotations.CollectionBinder) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with NotFoundAction

use of org.hibernate.annotations.NotFoundAction in project hibernate-orm by hibernate.

the class MappingModelCreationHelper method interpretElement.

private static CollectionPart interpretElement(Collection bootDescriptor, String tableExpression, CollectionPersister collectionDescriptor, String sqlAliasStem, Dialect dialect, MappingModelCreationProcess creationProcess) {
    final Value element = bootDescriptor.getElement();
    if (element instanceof BasicValue) {
        final BasicValue basicElement = (BasicValue) element;
        final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, basicElement.getSelectables().get(0), basicElement.resolve().getJdbcMapping(), creationProcess.getCreationContext().getTypeConfiguration(), dialect, creationProcess.getSqmFunctionRegistry());
        return new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.ELEMENT, basicElement.resolve().getValueConverter(), selectableMapping);
    }
    if (element instanceof Component) {
        final Component component = (Component) element;
        final CompositeType compositeType = (CompositeType) collectionDescriptor.getElementType();
        final EmbeddableMappingTypeImpl mappingType = EmbeddableMappingTypeImpl.from(component, compositeType, embeddableMappingType -> new EmbeddedCollectionPart(collectionDescriptor, CollectionPart.Nature.ELEMENT, embeddableMappingType, // parent-injection
        component.getParentProperty(), tableExpression, sqlAliasStem), creationProcess);
        return (CollectionPart) mappingType.getEmbeddedValueMapping();
    }
    if (element instanceof Any) {
        final Any anyBootMapping = (Any) element;
        final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
        final TypeConfiguration typeConfiguration = sessionFactory.getTypeConfiguration();
        final JavaTypeRegistry jtdRegistry = typeConfiguration.getJavaTypeRegistry();
        final JavaType<Object> baseJtd = jtdRegistry.getDescriptor(Object.class);
        return new DiscriminatedCollectionPart(CollectionPart.Nature.ELEMENT, collectionDescriptor, baseJtd, anyBootMapping, anyBootMapping.getType(), creationProcess);
    }
    if (element instanceof OneToMany || element instanceof ToOne) {
        final EntityType elementEntityType = (EntityType) collectionDescriptor.getElementType();
        final EntityPersister associatedEntity = creationProcess.getEntityPersister(elementEntityType.getAssociatedEntityName());
        final NotFoundAction notFoundAction;
        if (element instanceof ManyToOne) {
            notFoundAction = ((ManyToOne) element).getNotFoundAction();
        } else if (element instanceof OneToMany) {
            notFoundAction = ((OneToMany) element).getNotFoundAction();
        } else {
            throw new IllegalArgumentException("Just seeing if this happens");
        }
        final EntityCollectionPart elementDescriptor = new EntityCollectionPart(collectionDescriptor, CollectionPart.Nature.ELEMENT, bootDescriptor.getElement(), notFoundAction, associatedEntity, creationProcess);
        creationProcess.registerInitializationCallback("PluralAttributeMapping( " + elementDescriptor.getNavigableRole() + ") - index descriptor", () -> {
            elementDescriptor.finishInitialization(collectionDescriptor, bootDescriptor, elementEntityType.getRHSUniqueKeyPropertyName(), creationProcess);
            return true;
        });
        return elementDescriptor;
    }
    throw new NotYetImplementedFor6Exception("Support for plural attributes with element type [" + element + "] not yet implemented");
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) Any(org.hibernate.mapping.Any) ManyToOne(org.hibernate.mapping.ManyToOne) BasicValue(org.hibernate.mapping.BasicValue) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) ToOne(org.hibernate.mapping.ToOne) OneToOne(org.hibernate.mapping.OneToOne) ManyToOne(org.hibernate.mapping.ManyToOne) Component(org.hibernate.mapping.Component) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) NotFoundAction(org.hibernate.annotations.NotFoundAction) OneToMany(org.hibernate.mapping.OneToMany) EntityType(org.hibernate.type.EntityType) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) BasicValue(org.hibernate.mapping.BasicValue) SortableValue(org.hibernate.mapping.SortableValue) KeyValue(org.hibernate.mapping.KeyValue) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) CompositeType(org.hibernate.type.CompositeType)

Example 4 with NotFoundAction

use of org.hibernate.annotations.NotFoundAction in project hibernate-orm by hibernate.

the class AnnotationBinder method bindOneToOne.

private static void bindOneToOne(PropertyHolder propertyHolder, PropertyData inferredData, boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext context, XProperty property, AnnotatedJoinColumn[] joinColumns, PropertyBinder propertyBinder, boolean forcePersist) {
    OneToOne ann = property.getAnnotation(OneToOne.class);
    // check validity
    if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
        throw new AnnotationException("@Column(s) not allowed on a @OneToOne property: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    // FIXME support a proper PKJCs
    boolean trueOneToOne = property.isAnnotationPresent(PrimaryKeyJoinColumn.class) || property.isAnnotationPresent(PrimaryKeyJoinColumns.class);
    Cascade hibernateCascade = property.getAnnotation(Cascade.class);
    NotFound notFound = property.getAnnotation(NotFound.class);
    NotFoundAction notFoundAction = notFound == null ? null : notFound.action();
    final boolean hasNotFoundAction = notFoundAction != null;
    // MapsId means the columns belong to the pk;
    // A @MapsId association (obviously) must be non-null when the entity is first persisted.
    // If a @MapsId association is not mapped with @NotFound(IGNORE), then the association
    // is mandatory (even if the association has optional=true).
    // If a @MapsId association has optional=true and is mapped with @NotFound(IGNORE) then
    // the association is optional.
    // @OneToOne(optional = true) with @PKJC makes the association optional.
    final boolean mandatory = !ann.optional() || property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(MapsId.class) && !hasNotFoundAction;
    matchIgnoreNotFoundWithFetchType(propertyHolder.getEntityName(), property.getName(), notFoundAction, ann.fetch());
    OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
    JoinTable assocTable = propertyHolder.getJoinTable(property);
    if (assocTable != null) {
        Join join = propertyHolder.addJoin(assocTable, false);
        if (hasNotFoundAction) {
            join.disableForeignKeyCreation();
        }
        for (AnnotatedJoinColumn joinColumn : joinColumns) {
            joinColumn.setExplicitTableName(join.getTable().getName());
        }
    }
    bindOneToOne(getCascadeStrategy(ann.cascade(), hibernateCascade, ann.orphanRemoval(), forcePersist), joinColumns, !mandatory, getFetchMode(ann.fetch()), notFoundAction, onDeleteAnn != null && OnDeleteAction.CASCADE == onDeleteAnn.action(), ToOneBinder.getTargetEntity(inferredData, context), propertyHolder, inferredData, ann.mappedBy(), trueOneToOne, isIdentifierMapper, inSecondPass, propertyBinder, context);
}
Also used : AnnotatedJoinColumn.buildJoinTableJoinColumns(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns) JoinColumns(jakarta.persistence.JoinColumns) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) Join(org.hibernate.mapping.Join) NotFoundAction(org.hibernate.annotations.NotFoundAction) OneToOne(jakarta.persistence.OneToOne) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) MapKeyColumn(jakarta.persistence.MapKeyColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) Column(jakarta.persistence.Column) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) OrderColumn(jakarta.persistence.OrderColumn) DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) AnnotatedDiscriminatorColumn.buildDiscriminatorColumn(org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn) AnnotationException(org.hibernate.AnnotationException) Cascade(org.hibernate.annotations.Cascade) NotFound(org.hibernate.annotations.NotFound) OnDelete(org.hibernate.annotations.OnDelete) JoinTable(jakarta.persistence.JoinTable)

Aggregations

NotFoundAction (org.hibernate.annotations.NotFoundAction)4 Column (jakarta.persistence.Column)2 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)2 JoinColumn (jakarta.persistence.JoinColumn)2 JoinColumns (jakarta.persistence.JoinColumns)2 JoinTable (jakarta.persistence.JoinTable)2 MapKeyColumn (jakarta.persistence.MapKeyColumn)2 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)2 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)2 OrderColumn (jakarta.persistence.OrderColumn)2 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)2 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)2 AnnotationException (org.hibernate.AnnotationException)2 Cascade (org.hibernate.annotations.Cascade)2 Columns (org.hibernate.annotations.Columns)2 NotFound (org.hibernate.annotations.NotFound)2 OnDelete (org.hibernate.annotations.OnDelete)2 AnnotatedDiscriminatorColumn.buildDiscriminatorColumn (org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn)2 AnnotatedJoinColumn.buildJoinTableJoinColumns (org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns)2 Join (org.hibernate.mapping.Join)2