Skip to content

Commit d530de4

Browse files
committed
HHH-15060 - Associations with @NotFound should always be left joined when de-referenced in HQL/Criteria
- `@NotFound` no longer exports a physical foreign-key - tests showing bugs and inconsistencies wrt `@NotFound` handling - added `FetchNotFoundException` - consider a to-one nullable if either: - explicitly marked nullable - `@NotFound` is specified, whether IGNORE or EXCEPTION - force association to EAGER
1 parent c200eaa commit d530de4

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

hibernate-core/src/main/java/org/hibernate/type/ManyToOneType.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
import org.hibernate.annotations.NotFoundAction;
2020
import org.hibernate.engine.internal.ForeignKeys;
2121
import org.hibernate.engine.jdbc.Size;
22-
import org.hibernate.engine.spi.*;
22+
import org.hibernate.engine.spi.EntityEntry;
23+
import org.hibernate.engine.spi.EntityKey;
24+
import org.hibernate.engine.spi.EntityUniqueKey;
25+
import org.hibernate.engine.spi.Mapping;
26+
import org.hibernate.engine.spi.PersistenceContext;
27+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2328
import org.hibernate.persister.entity.EntityPersister;
2429
import org.hibernate.persister.entity.Loadable;
2530

@@ -30,6 +35,7 @@
3035
*/
3136
public class ManyToOneType extends EntityType {
3237
private final String propertyName;
38+
private final boolean nullable;
3339
private final NotFoundAction notFoundAction;
3440
private boolean isLogicalOneToOne;
3541

@@ -57,7 +63,7 @@ public ManyToOneType(TypeFactory.TypeScope scope, String referencedEntityName, b
5763

5864

5965
/**
60-
* @deprecated Use {@link #ManyToOneType(TypeFactory.TypeScope, String, boolean, String, String, boolean, boolean, NotFoundAction, boolean ) } instead.
66+
* @deprecated Use {@link #ManyToOneType(TypeFactory.TypeScope, String, boolean, String, String, boolean, boolean, boolean, NotFoundAction, boolean ) } instead.
6167
*/
6268
@Deprecated
6369
public ManyToOneType(
@@ -73,7 +79,7 @@ public ManyToOneType(
7379
}
7480

7581
/**
76-
* @deprecated Use {@link #ManyToOneType(TypeFactory.TypeScope, String, boolean, String, String, boolean, boolean, NotFoundAction, boolean ) } instead.
82+
* @deprecated Use {@link #ManyToOneType(TypeFactory.TypeScope, String, boolean, String, String, boolean, boolean, boolean, NotFoundAction, boolean ) } instead.
7783
*/
7884
@Deprecated
7985
public ManyToOneType(
@@ -85,7 +91,18 @@ public ManyToOneType(
8591
boolean unwrapProxy,
8692
NotFoundAction notFoundAction,
8793
boolean isLogicalOneToOne) {
88-
this( scope, referencedEntityName, referenceToPrimaryKey, uniqueKeyPropertyName, null, lazy, unwrapProxy, notFoundAction, isLogicalOneToOne );
94+
this(
95+
scope,
96+
referencedEntityName,
97+
referenceToPrimaryKey,
98+
uniqueKeyPropertyName,
99+
null,
100+
notFoundAction != null,
101+
lazy,
102+
unwrapProxy,
103+
notFoundAction,
104+
isLogicalOneToOne
105+
);
89106
}
90107

91108
public ManyToOneType(
@@ -94,13 +111,15 @@ public ManyToOneType(
94111
boolean referenceToPrimaryKey,
95112
String uniqueKeyPropertyName,
96113
String propertyName,
114+
boolean nullable,
97115
boolean lazy,
98116
boolean unwrapProxy,
99117
NotFoundAction notFoundAction,
100118
boolean isLogicalOneToOne) {
101119
super( scope, referencedEntityName, referenceToPrimaryKey, uniqueKeyPropertyName, !lazy, unwrapProxy );
102120
this.propertyName = propertyName;
103121
this.notFoundAction = notFoundAction;
122+
this.nullable = nullable;
104123
this.isLogicalOneToOne = isLogicalOneToOne;
105124
}
106125

@@ -109,12 +128,12 @@ public ManyToOneType(ManyToOneType original, String superTypeEntityName) {
109128
this.propertyName = original.propertyName;
110129
this.notFoundAction = original.notFoundAction;
111130
this.isLogicalOneToOne = original.isLogicalOneToOne;
131+
this.nullable = original.nullable;
112132
}
113133

114134
@Override
115135
public boolean isNullable() {
116-
// this matches the original check, but this seems wrong.
117-
return notFoundAction == NotFoundAction.IGNORE;
136+
return notFoundAction != null;
118137
}
119138

120139
@Override

hibernate-core/src/main/java/org/hibernate/type/TypeFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,31 @@ public EntityType manyToOne(
356356
referenceToPrimaryKey,
357357
uniqueKeyPropertyName,
358358
propertyName,
359+
notFoundAction != null,
360+
lazy,
361+
unwrapProxy,
362+
notFoundAction,
363+
isLogicalOneToOne
364+
);
365+
}
366+
367+
public EntityType manyToOne(
368+
String persistentClass,
369+
boolean referenceToPrimaryKey,
370+
String uniqueKeyPropertyName,
371+
String propertyName,
372+
boolean nullable,
373+
boolean lazy,
374+
boolean unwrapProxy,
375+
NotFoundAction notFoundAction,
376+
boolean isLogicalOneToOne) {
377+
return new ManyToOneType(
378+
typeScope,
379+
persistentClass,
380+
referenceToPrimaryKey,
381+
uniqueKeyPropertyName,
382+
propertyName,
383+
nullable,
359384
lazy,
360385
unwrapProxy,
361386
notFoundAction,

0 commit comments

Comments
 (0)