public IEntityPersister FindEntityPersisterUsingImports(string className)
        {
            // NH : short cut
            if (string.IsNullOrEmpty(className))
            {
                return(null);
            }

            if (!char.IsLetter(className[0]) && !className[0].Equals('_'))
            {
                return(null);
            }

            // NH : this method prevent unrecognized class when entityName != class.FullName
            // this is a patch for the TODO below
            var possibleResult = sfi.TryGetEntityPersister(GetEntityName(className));

            if (possibleResult != null)
            {
                return(possibleResult);
            }

            string importedClassName = sfi.GetImportedClassName(className);

            if (importedClassName == null)
            {
                return(null);
            }
            // NH: This method don't work if entityName != class.FullName
            return(sfi.TryGetEntityPersister(GetEntityName(importedClassName)));
        }
        private static bool TryGetEntityPersister(
            string currentEntityName,
            System.Type convertedType,
            ISessionFactoryImplementor sessionFactory,
            out IEntityPersister persister)
        {
            var currentEntityPersister = sessionFactory.TryGetEntityPersister(currentEntityName);

            if (currentEntityPersister == null)
            {
                // When dealing with a polymorphic query it is not important which entity name we pick
                // as they all need to have the same mapped types for members of the type that is queried.
                // If one of the entites has a different type mapped (e.g. enum mapped as string instead of numeric),
                // the query will fail to execute as currently the ParameterMetadata is bound to IQueryPlan and not to IQueryTranslator
                // (e.g. s.Query<IEntity>().Where(a => a.MyEnum == MyEnum.Option)).
                currentEntityName = sessionFactory.GetImplementors(currentEntityName).FirstOrDefault();
                if (currentEntityName == null)
                {
                    persister = null;
                    return(false);
                }

                currentEntityPersister = sessionFactory.GetEntityPersister(currentEntityName);
            }

            return(TryGetEntityPersister(currentEntityPersister, convertedType, sessionFactory, out persister));
        }
Exemple #3
0
        protected virtual IEntityPersister GetEntityPersister(ISessionFactoryImplementor factory, string entityName)
        {
            // Check for an exact match.
            IEntityPersister persister = factory.TryGetEntityPersister(entityName);

            if (persister != null)
            {
                return(persister);
            }

            // Couldn't find persister through exact name, try finding a single implementing class.
            string[] implementors = factory.GetImplementors(entityName);
            if (implementors.Length > 1)
            {
                var messageBuilder = new StringBuilder(512);
                messageBuilder.AppendLine(string.Format("Ambiguous persister for {0} implemented by more than one hierarchy: ",
                                                        entityName));
                Array.ForEach(implementors, s => messageBuilder.AppendLine(s));

                throw new HibernateException(messageBuilder.ToString());
            }
            if (implementors.Length == 0)
            {
                return(null);
            }
            return(factory.GetEntityPersister(implementors[0]));
        }
        private IEntityPersister ResolvePersister(string entityName)
        {
            // Check for an exact match.

            var persister = _sessionFactory.TryGetEntityPersister(entityName);

            if (persister != null)
            {
                return(persister);
            }

            // Couldn't find persister through exact name, try finding a single implementing class.

            string[] implementors = _sessionFactory.GetImplementors(entityName);

            if (implementors.Length > 1)
            {
                throw new ODataException(String.Format(ErrorMessages.ODataRequest_AmbiguousEntityName, entityName));
            }
            else if (implementors.Length == 0)
            {
                return(null);
            }
            else
            {
                return(_sessionFactory.GetEntityPersister(implementors[0]));
            }
        }
        /// <summary>
        /// Guesses the <see cref="IType"/> from the <see cref="System.Type"/>.
        /// </summary>
        /// <param name="clazz">The <see cref="System.Type"/> to guess the <see cref="IType"/> of.</param>
        /// <param name="sessionFactory">The session factory to search for entity persister.</param>
        /// <returns>An <see cref="IType"/> for the <see cref="System.Type"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when the <c>clazz</c> is null because the <see cref="IType"/>
        /// can't be guess from a null type.
        /// </exception>
        public static IType TryGuessType(System.Type clazz, ISessionFactoryImplementor sessionFactory)
        {
            if (clazz == null)
            {
                return(null);
            }

            var type = TypeFactory.HeuristicType(clazz);

            if (type == null || type is SerializableType)
            {
                if (sessionFactory.TryGetEntityPersister(clazz.FullName) != null)
                {
                    return(NHibernateUtil.Entity(clazz));
                }
            }

            return(type);
        }
		protected virtual IEntityPersister GetEntityPersister(ISessionFactoryImplementor factory, string entityName)
		{
			// Check for an exact match.
			IEntityPersister persister = factory.TryGetEntityPersister(entityName);
			if (persister != null)
			{
				return persister;
			}

			// Couldn't find persister through exact name, try finding a single implementing class.
			string[] implementors = factory.GetImplementors(entityName);
			if (implementors.Length > 1)
			{
				var messageBuilder = new StringBuilder(512);
				messageBuilder.AppendLine(string.Format("Ambiguous persister for {0} implemented by more than one hierarchy: ",
				                                        entityName));
				Array.ForEach(implementors, s=> messageBuilder.AppendLine(s));

				throw new HibernateException(messageBuilder.ToString());
			}
			if (implementors.Length == 0)
			{
				return null;
			}
			return factory.GetEntityPersister(implementors[0]);
		}