@@ -71,28 +71,30 @@ default String getPartName() {
71
71
/**
72
72
* Extract the identifier from an instance of the entity
73
73
*
74
- * It's supposed to be use during the merging process
74
+ * @apiNote Intended for use during the merging process
75
75
*/
76
76
default Object getIdentifier (Object entity , MergeContext mergeContext ){
77
77
return getIdentifier ( entity );
78
78
}
79
79
80
80
/**
81
- * Return the identifier of the persistent or transient object, or throw
82
- * an exception if the instance is "unsaved"
83
- * <p>
84
- * Used by OneToOneType and ManyToOneType to determine what id value should
85
- * be used for an object that may or may not be associated with the session.
86
- * This does a "best guess" using any/all info available to use (not just the
87
- * EntityEntry).
81
+ * Return the identifier of the persistent or transient object, or throw an
82
+ * exception if the instance is "unsaved"
83
+ *
84
+ * @apiNote This method is called by {@link org.hibernate.type.OneToOneType}
85
+ * and {@link org.hibernate.type.ManyToOneType} to determine the id value
86
+ * which should be used for an object that may or may not be associated with
87
+ * the session. This does a "best guess" using any/all info available to use
88
+ * (not just the {@link org.hibernate.engine.spi.EntityEntry}).
88
89
*
89
90
* @param entity The entity instance
90
91
* @param session The session
91
92
*
92
93
* @return The identifier
93
94
*
94
- * @throws TransientObjectException if the entity is transient (does not yet have an identifier)
95
- * @see org.hibernate.engine.internal.ForeignKeys#getEntityIdentifierIfNotUnsaved(String, Object, SharedSessionContractImplementor)
95
+ * @throws TransientObjectException if the entity is transient
96
+ * (does not yet have an identifier)
97
+ * @see org.hibernate.engine.internal.ForeignKeys#getEntityIdentifierIfNotUnsaved
96
98
* @since 6.1.1
97
99
*/
98
100
default Object getIdentifierIfNotUnsaved (Object entity , SharedSessionContractImplementor session ) {
@@ -103,20 +105,23 @@ else if ( session == null ) {
103
105
// If we have no session available, just return the identifier
104
106
return getIdentifier ( entity );
105
107
}
106
- Object id = session .getContextEntityIdentifier ( entity );
108
+ final Object id = session .getContextEntityIdentifier ( entity );
107
109
if ( id == null ) {
108
- // context-entity-identifier returns null explicitly if the entity
109
- // is not associated with the persistence context; so make some
110
- // deeper checks...
110
+ // getContextEntityIdentifier() returned null, indicating that
111
+ // the entity is not associated with the persistence context,
112
+ // so look deeper for its id
111
113
final String entityName = findContainingEntityMapping ().getEntityName ();
112
114
if ( ForeignKeys .isTransient ( entityName , entity , Boolean .FALSE , session ) ) {
115
+ // TODO should be a TransientPropertyValueException
113
116
throw new TransientObjectException ( "object references an unsaved transient instance of '"
114
117
+ (entityName == null ? session .guessEntityName ( entity ) : entityName )
115
118
+ "' save the transient instance before flushing" );
116
119
}
117
- id = getIdentifier ( entity );
120
+ return getIdentifier ( entity );
121
+ }
122
+ else {
123
+ return id ;
118
124
}
119
- return id ;
120
125
}
121
126
122
127
/**
0 commit comments