From a7261ad0533f8cc1eabd299a62e5daef8735faae Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 21 Feb 2020 15:32:29 -0800 Subject: [PATCH] HHH-13875 : Optional one-to-one does not always join the associated entity table when querying --- .../org/hibernate/hql/internal/ast/tree/DotNode.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/DotNode.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/DotNode.java index 71cb7d8c99..426b5c6838 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/DotNode.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/DotNode.java @@ -394,11 +394,14 @@ public class DotNode extends FromReferenceNode implements DisplayableNode, Selec if ( isDotNode( parent ) ) { // our parent is another dot node, meaning we are being further dereferenced. - // thus we need to generate a join unless the parent refers to the associated - // entity's PK (because 'our' table would know the FK). + // thus we need to generate a join unless the association is non-nullable and + // parent refers to the associated entity's PK (because 'our' table would know the FK). parentAsDotNode = (DotNode) parent; property = parentAsDotNode.propertyName; - joinIsNeeded = generateJoin && !isPropertyEmbeddedInJoinProperties( parentAsDotNode.propertyName ); + joinIsNeeded = generateJoin && ( + entityType.isNullable() || + !isPropertyEmbeddedInJoinProperties( parentAsDotNode.propertyName ) + ); } else if ( !getWalker().isSelectStatement() ) { // in non-select queries, the only time we should need to join is if we are in a subquery from clause