Esempio n. 1
0
        protected internal object GetIdentifier(object value, ISessionImplementor session)
        {
            if (IsNotEmbedded(session))
            {
                return(value);
            }

            return(ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session));            //tolerates nulls
        }
Esempio n. 2
0
 private static object Id(object component, ISessionImplementor session)
 {
     try
     {
         return(ForeignKeys.GetEntityIdentifierIfNotUnsaved(session.BestGuessEntityName(component), component, session));
     }
     catch (TransientObjectException)
     {
         return(null);
     }
 }
Esempio n. 3
0
        public object Disassemble(Object value, ISessionImplementor session)
        {
            if (value == null)
            {
                return(null);
            }

            Multiplicity m = (Multiplicity)value;

            return(new object[] { m.count, ForeignKeys.GetEntityIdentifierIfNotUnsaved(null, m.glarch, session) });
        }
Esempio n. 4
0
 public override object Replace(object original, object current, ISessionImplementor session, object owner,
                                IDictionary copiedAlready)
 {
     if (original == null)
     {
         return(null);
     }
     else
     {
         string entityName = session.BestGuessEntityName(original);
         object id         = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, original, session);
         return(session.InternalLoad(entityName, id, false, false));
     }
 }
Esempio n. 5
0
        public override object Disassemble(object value, ISessionImplementor session, object owner)
        {
            if (value == null)
            {
                return(null);
            }

            object id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session);

            if (id == null)
            {
                throw new AssertionFailure("cannot cache a reference to an object with a null id: " + GetAssociatedEntityName());
            }

            return(GetIdentifierType(session).Disassemble(id, session, owner));
        }
Esempio n. 6
0
        /// <summary>
        /// Generates an identifier from the value of a Property.
        /// </summary>
        /// <param name="sessionImplementor">The <see cref="ISessionImplementor"/> this id is being generated in.</param>
        /// <param name="obj">The entity for which the id is being generated.</param>
        /// <returns>
        /// The identifier value from the associated object or
        /// <see cref="IdentifierGeneratorFactory.ShortCircuitIndicator"/> if the <c>session</c>
        /// already contains <c>obj</c>.
        /// </returns>
        public object Generate(ISessionImplementor sessionImplementor, object obj)
        {
            ISession session = (ISession)sessionImplementor;

            var    persister        = sessionImplementor.Factory.GetEntityPersister(entityName);
            object associatedObject = persister.GetPropertyValue(obj, propertyName);

            if (associatedObject == null)
            {
                throw new IdentifierGenerationException("attempted to assign id from null one-to-one property: " + propertyName);
            }

            EntityType foreignValueSourceType;
            IType      propertyType = persister.GetPropertyType(propertyName);

            if (propertyType.IsEntityType)
            {
                foreignValueSourceType = (EntityType)propertyType;
            }
            else
            {
                // try identifier mapper
                foreignValueSourceType = (EntityType)persister.GetPropertyType("_identifierMapper." + propertyName);
            }

            object id;

            try
            {
                id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(
                    foreignValueSourceType.GetAssociatedEntityName(),
                    associatedObject,
                    sessionImplementor);
            }
            catch (TransientObjectException)
            {
                id = session.Save(foreignValueSourceType.GetAssociatedEntityName(), associatedObject);
            }

            if (session.Contains(obj))
            {
                //abort the save (the object is already saved by a circular cascade)
                return(IdentifierGeneratorFactory.ShortCircuitIndicator);
            }

            return(id);
        }
Esempio n. 7
0
        /// <summary>
        /// Generates an identifier from the value of a Property.
        /// </summary>
        /// <param name="sessionImplementor">The <see cref="ISessionImplementor"/> this id is being generated in.</param>
        /// <param name="obj">The entity for which the id is being generated.</param>
        /// <returns>
        /// The identifier value from the associated object or
        /// <see cref="IdentifierGeneratorFactory.ShortCircuitIndicator"/> if the <c>session</c>
        /// already contains <c>obj</c>.
        /// </returns>
        public object Generate(ISessionImplementor sessionImplementor, object obj)
        {
            var    persister        = sessionImplementor.Factory.GetEntityPersister(entityName);
            object associatedObject = persister.GetPropertyValue(obj, propertyName);

            if (associatedObject == null)
            {
                throw new IdentifierGenerationException("attempted to assign id from null one-to-one property: " + propertyName);
            }

            var foreignValueSourceType = GetForeignValueSourceType(persister);

            object id;

            try
            {
                id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(
                    foreignValueSourceType.GetAssociatedEntityName(),
                    associatedObject,
                    sessionImplementor);
            }
            catch (TransientObjectException)
            {
                if (sessionImplementor is ISession session)
                {
                    id = session.Save(foreignValueSourceType.GetAssociatedEntityName(), associatedObject);
                }
                else if (sessionImplementor is IStatelessSession statelessSession)
                {
                    id = statelessSession.Insert(foreignValueSourceType.GetAssociatedEntityName(), associatedObject);
                }
                else
                {
                    throw new IdentifierGenerationException("sessionImplementor is neither Session nor StatelessSession");
                }
            }

            if (Contains(sessionImplementor, obj))
            {
                //abort the save (the object is already saved by a circular cascade)
                return(IdentifierGeneratorFactory.ShortCircuitIndicator);
            }

            return(id);
        }
Esempio n. 8
0
 public override object Disassemble(object value, ISessionImplementor session, object owner)
 {
     if (value == null)
     {
         return(null);
     }
     else
     {
         // cache the actual id of the object, not the value of the
         // property-ref, which might not be initialized
         object id = ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session);
         if (id == null)
         {
             throw new AssertionFailure("cannot cache a reference to an object with a null id: " + GetAssociatedEntityName());
         }
         return(GetIdentifierType(session).Disassemble(id, session, owner));
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Given a collection of entity instances that used to
        /// belong to the collection, and a collection of instances
        /// that currently belong, return a collection of orphans
        /// </summary>
        protected virtual ICollection GetOrphans(ICollection oldElements, ICollection currentElements, string entityName,
                                                 ISessionImplementor session)
        {
            // short-circuit(s)
            if (currentElements.Count == 0)
            {
                // no new elements, the old list contains only Orphans
                return(oldElements);
            }
            if (oldElements.Count == 0)
            {
                // no old elements, so no Orphans neither
                return(oldElements);
            }

            IType idType = session.Factory.GetEntityPersister(entityName).IdentifierType;

            // create the collection holding the orphans
            List <object> res = new List <object>();

            // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
            var currentIds = new HashSet <TypedValue>();

            foreach (object current in currentElements)
            {
                if (current != null && ForeignKeys.IsNotTransient(entityName, current, null, session))
                {
                    object currentId = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, current, session);
                    currentIds.Add(new TypedValue(idType, currentId, session.EntityMode));
                }
            }

            // iterate over the *old* list
            foreach (object old in oldElements)
            {
                object oldId = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, old, session);
                if (!currentIds.Contains(new TypedValue(idType, oldId, session.EntityMode)))
                {
                    res.Add(old);
                }
            }

            return(res);
        }
Esempio n. 10
0
 public override object Disassemble(object value, ISessionImplementor session, object owner)
 {
     return(value == null ? null :
            new ObjectTypeCacheEntry(session.BestGuessEntityName(value),
                                     ForeignKeys.GetEntityIdentifierIfNotUnsaved(session.BestGuessEntityName(value), value, session)));
 }