예제 #1
0
        /// <summary>
        /// Get the key value from the owning entity instance, usually the identifier, but might be some
        /// other unique key, in the case of property-ref
        /// </summary>
        public object GetKeyOfOwner(object owner, ISessionImplementor session)
        {
            EntityEntry entityEntry = session.PersistenceContext.GetEntry(owner);

            if (entityEntry == null)
            {
                return(null);                // This just handles a particular case of component
            }
            // projection, perhaps get rid of it and throw an exception

            if (foreignKeyPropertyName == null)
            {
                return(entityEntry.Id);
            }
            else
            {
                // TODO: at the point where we are resolving collection references, we don't
                // know if the uk value has been resolved (depends if it was earlier or
                // later in the mapping document) - now, we could try and use e.getStatus()
                // to decide to semiResolve(), trouble is that initializeEntity() reuses
                // the same array for resolved and hydrated values
                object id;
                if (entityEntry.LoadedState != null)
                {
                    id = entityEntry.GetLoadedValue(foreignKeyPropertyName);
                }
                else
                {
                    id = entityEntry.Persister.GetPropertyValue(owner, foreignKeyPropertyName, session.EntityMode);
                }

                // NOTE VERY HACKISH WORKAROUND!!
                IType keyType = GetPersister(session).KeyType;
                if (!keyType.ReturnedClass.IsInstanceOfType(id))
                {
                    id = keyType.SemiResolve(entityEntry.GetLoadedValue(foreignKeyPropertyName), session, owner);
                }

                return(id);
            }
        }