/// <summary> /// Get the identifier value for the entity. If the entity does not have an /// identifier property, or natural identifiers defined, then the entity itself is returned. /// </summary> /// <param name="entity"></param> /// <param name="meta"></param> /// <returns></returns> private object GetIdentifier(object entity, IClassMetadata meta = null) { var type = entity.GetType(); meta = meta ?? session.SessionFactory.GetClassMetadata(type); if (meta.IdentifierType != null) { var id = meta.GetIdentifier(entity, EntityMode.Poco); if (meta.IdentifierType.IsComponentType) { var compType = (ComponentType)meta.IdentifierType; return(compType.GetPropertyValues(id, EntityMode.Poco)); } else { return(id); } } else if (meta.HasNaturalIdentifier) { var idprops = meta.NaturalIdentifierProperties; var values = meta.GetPropertyValues(entity, EntityMode.Poco); var idvalues = idprops.Select(i => values[i]).ToArray(); return(idvalues); } return(entity); }
/// <summary> /// /// </summary> /// <param name="entity">an actual entity object, not a proxy!</param> /// <param name="entityMode"></param> /// <returns></returns> public string ToString(object entity, EntityMode entityMode) { IClassMetadata cm = _factory.GetClassMetadata(entity.GetType()); if (cm == null) { return(entity.GetType().FullName); } IDictionary <string, string> result = new Dictionary <string, string>(); if (cm.HasIdentifierProperty) { result[cm.IdentifierPropertyName] = cm.IdentifierType.ToLoggableString(cm.GetIdentifier(entity, entityMode), _factory); } IType[] types = cm.PropertyTypes; string[] names = cm.PropertyNames; object[] values = cm.GetPropertyValues(entity, entityMode); for (int i = 0; i < types.Length; i++) { result[names[i]] = types[i].ToLoggableString(values[i], _factory); } return(cm.EntityName + CollectionPrinter.ToString(result)); }
public override TypedValue[] GetTypedValues(ISessionFactoryImplementor sessionFactory, System.Type persistentClass, IDictionary aliasClasses) { IClassMetadata meta = sessionFactory.GetClassMetadata(persistentClass); string[] propertyNames = meta.PropertyNames; IType[] propertyTypes = meta.PropertyTypes; object[] values = meta.GetPropertyValues(_entity); ArrayList list = new ArrayList(); for (int i = 0; i < propertyNames.Length; i++) { object value = values[i]; IType type = propertyTypes[i]; string name = propertyNames[i]; bool isPropertyIncluded = (i != meta.VersionProperty && IsPropertyIncluded(value, name, type)); if (isPropertyIncluded) { if (propertyTypes[i].IsComponentType) { AddComponentTypedValues(name, value, (IAbstractComponentType)type, list); } else { AddPropertyTypedValue(value, type, list); } } } return((TypedValue[])list.ToArray(typeof(TypedValue))); }
/// <summary> /// Get the identifier value for the entity. If the entity does not have an /// identifier property, or natural identifiers defined, then the entity itself is returned. /// </summary> /// <param name="entity"></param> /// <param name="meta"></param> /// <returns></returns> protected object GetIdentifier(object entity, IClassMetadata meta = null) { var type = Session.GetProxyRealType(entity); meta = meta ?? Session.SessionFactory.GetClassMetadata(type); if (meta.IdentifierType != null) { var id = meta.GetIdentifier(entity); if (meta.IdentifierType.IsComponentType) { var compType = (ComponentType)meta.IdentifierType; return(compType.GetPropertyValues(id)); } return(id); } if (meta.HasNaturalIdentifier) { var idprops = meta.NaturalIdentifierProperties; var values = meta.GetPropertyValues(entity); var idvalues = idprops.Select(i => values[i]).ToArray(); return(idvalues); } return(entity); }
public override SqlString ToSqlString( ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses) { SqlStringBuilder builder = new SqlStringBuilder(); builder.Add(StringHelper.OpenParen); IClassMetadata meta = factory.GetClassMetadata(persistentClass); String[] propertyNames = meta.PropertyNames; IType[] propertyTypes = meta.PropertyTypes; object[] propertyValues = meta.GetPropertyValues(_entity); for (int i = 0; i < propertyNames.Length; i++) { object propertyValue = propertyValues[i]; String propertyName = propertyNames[i]; bool isPropertyIncluded = i != meta.VersionProperty && IsPropertyIncluded(propertyValue, propertyName, propertyTypes[i]); if (isPropertyIncluded) { if (propertyTypes[i].IsComponentType) { AppendComponentCondition( propertyName, propertyValue, (IAbstractComponentType)propertyTypes[i], persistentClass, alias, aliasClasses, factory, builder ); } else { AppendPropertyCondition( propertyName, propertyValue, persistentClass, alias, aliasClasses, factory, builder ); } } } if (builder.Count == 1) { builder.Add("1=1"); // yuck! } builder.Add(StringHelper.ClosedParen); return(builder.ToSqlString()); }
/// <summary> /// /// </summary> /// <param name="entity">an actual entity object, not a proxy!</param> /// <returns></returns> public string ToString(object entity) { IClassMetadata cm = _factory.GetClassMetadata(entity.GetType()); if (cm == null) { return(entity.GetType().FullName); } IDictionary <string, string> result = new Dictionary <string, string>(); if (cm.HasIdentifierProperty) { result[cm.IdentifierPropertyName] = cm.IdentifierType.ToLoggableString(cm.GetIdentifier(entity), _factory); } IType[] types = cm.PropertyTypes; string[] names = cm.PropertyNames; object[] values = cm.GetPropertyValues(entity); for (int i = 0; i < types.Length; i++) { var value = values[i]; if (Equals(LazyPropertyInitializer.UnfetchedProperty, value) || Equals(BackrefPropertyAccessor.Unknown, value)) { result[names[i]] = value.ToString(); } else { result[names[i]] = types[i].ToLoggableString(value, _factory); } } return(cm.EntityName + CollectionPrinter.ToString(result)); }