Skip to content

Commit

Permalink
HHH-15099 - Improve handling of associations marked with @NotFound
Browse files Browse the repository at this point in the history
- support for NotFound on logical 1-1 defined on JoinTable
  • Loading branch information
sebersole committed Mar 5, 2022
1 parent ed5831f commit 82feac6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
Expand Up @@ -2653,6 +2653,7 @@ private static void bindOneToOne(PropertyHolder propertyHolder, PropertyData inf
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.
Expand All @@ -2663,12 +2664,15 @@ private static void bindOneToOne(PropertyHolder propertyHolder, PropertyData inf
// @OneToOne(optional = true) with @PKJC makes the association optional.
final boolean mandatory = !ann.optional()
|| property.isAnnotationPresent( Id.class )
|| property.isAnnotationPresent( MapsId.class ) && notFoundAction != NotFoundAction.IGNORE;
|| 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() );
}
Expand Down
Expand Up @@ -275,6 +275,11 @@ private Join buildJoinFromMappedBySide(PersistentClass persistentClass, Property
join.setTable( originalJoin.getTable() );
join.setInverse( true );
DependantValue key = new DependantValue( buildingContext, join.getTable(), persistentClass.getIdentifier() );

if ( notFoundAction != null ) {
join.disableForeignKeyCreation();
}

//TODO support @ForeignKey
join.setKey( key );
join.setSequentialSelect( false );
Expand Down
Expand Up @@ -29,6 +29,5 @@ public SecondaryTableSecondPass(EntityBinder entityBinder, PropertyHolder proper

public void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
entityBinder.finalSecondaryTableBinding( propertyHolder );

}
}
10 changes: 9 additions & 1 deletion hibernate-core/src/main/java/org/hibernate/mapping/Join.java
Expand Up @@ -29,6 +29,7 @@ public class Join implements AttributeContainer, Serializable {
private boolean sequentialSelect;
private boolean inverse;
private boolean optional;
private boolean disableForeignKeyCreation;

// Custom SQL
private String customSQLInsert;
Expand Down Expand Up @@ -97,8 +98,15 @@ public void setPersistentClass(PersistentClass persistentClass) {
this.persistentClass = persistentClass;
}

public void disableForeignKeyCreation() {
disableForeignKeyCreation = true;
}

public void createForeignKey() {
getKey().createForeignKeyOfEntity( persistentClass.getEntityName() );
final ForeignKey foreignKey = getKey().createForeignKeyOfEntity( persistentClass.getEntityName() );
if ( disableForeignKeyCreation ) {
foreignKey.disableCreation();
}
}

public void createPrimaryKey() {
Expand Down
Expand Up @@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.mapping;

import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.id.IdentifierGenerator;
Expand Down Expand Up @@ -37,7 +38,7 @@ IdentifierGenerator createIdentifierGenerator(

boolean isIdentityColumn(IdentifierGeneratorFactory identifierGeneratorFactory, Dialect dialect);

void createForeignKeyOfEntity(String entityName);
ForeignKey createForeignKeyOfEntity(String entityName);

boolean isCascadeDeleteEnabled();

Expand Down
Expand Up @@ -16,6 +16,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import jakarta.persistence.AttributeConverter;

import org.hibernate.AssertionFailure;
import org.hibernate.FetchMode;
Expand Down Expand Up @@ -60,8 +61,6 @@
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.usertype.DynamicParameterizedType;

import jakarta.persistence.AttributeConverter;

/**
* Any value that maps to columns.
* @author Gavin King
Expand Down Expand Up @@ -321,11 +320,14 @@ public void setTable(Table table) {
public void createForeignKey() throws MappingException {}

@Override
public void createForeignKeyOfEntity(String entityName) {
public ForeignKey createForeignKeyOfEntity(String entityName) {
if ( isConstrained() ) {
table.createForeignKey( getForeignKeyName(), getConstraintColumns(), entityName, getForeignKeyDefinition() )
.setCascadeDeleteEnabled(cascadeDeleteEnabled);
final ForeignKey fk = table.createForeignKey( getForeignKeyName(), getConstraintColumns(), entityName, getForeignKeyDefinition() );
fk.setCascadeDeleteEnabled( cascadeDeleteEnabled );
return fk;
}

return null;
}

@Override
Expand Down

0 comments on commit 82feac6

Please sign in to comment.