@@ -71,28 +71,30 @@ default String getPartName() {
7171 /**
7272 * Extract the identifier from an instance of the entity
7373 *
74- * It's supposed to be use during the merging process
74+ * @apiNote Intended for use during the merging process
7575 */
7676 default Object getIdentifier (Object entity , MergeContext mergeContext ){
7777 return getIdentifier ( entity );
7878 }
7979
8080 /**
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}).
8889 *
8990 * @param entity The entity instance
9091 * @param session The session
9192 *
9293 * @return The identifier
9394 *
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
9698 * @since 6.1.1
9799 */
98100 default Object getIdentifierIfNotUnsaved (Object entity , SharedSessionContractImplementor session ) {
@@ -103,20 +105,23 @@ else if ( session == null ) {
103105 // If we have no session available, just return the identifier
104106 return getIdentifier ( entity );
105107 }
106- Object id = session .getContextEntityIdentifier ( entity );
108+ final Object id = session .getContextEntityIdentifier ( entity );
107109 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
111113 final String entityName = findContainingEntityMapping ().getEntityName ();
112114 if ( ForeignKeys .isTransient ( entityName , entity , Boolean .FALSE , session ) ) {
115+ // TODO should be a TransientPropertyValueException
113116 throw new TransientObjectException ( "object references an unsaved transient instance of '"
114117 + (entityName == null ? session .guessEntityName ( entity ) : entityName )
115118 + "' save the transient instance before flushing" );
116119 }
117- id = getIdentifier ( entity );
120+ return getIdentifier ( entity );
121+ }
122+ else {
123+ return id ;
118124 }
119- return id ;
120125 }
121126
122127 /**
0 commit comments