Ich habe eine Situation, in der einer meiner Tabelle selbst zugeordnet ist. Der Primärschlüssel einer Zeile (Parent) kann als Fremdschlüssel für eine andere Zeile (Child) verwendet werden, und diese Fremdschlüsselspalte enthält null für solche Zeilen, die kein Elternelement haben. Etwas wie folgt aus:Abrufen von Datensätzen mit Nullwert - fließend nhibernate
table: Settings_LocationType
++++++++++++++++++++++++++++++++++++++++++++++++
LocationID | Name | ParentLocationId
++++++++++++++++++++++++++++++++++++++++++++++++
1 Parent 1 null
2 Child 1 1
3 Child 2 1
4 Parent 2 null
Modell: location
public class LocationType
{
public virtual long LocationTypeId { get; set; }
public virtual string Name { get; set; }
public virtual LocationType ParentLocationType { get; set; }
public virtual IList<LocationType> LocationTypes { get; set; }
public LocationType()
{
LocationTypes = new List<LocationType>();
}
}
Mapping: LocationTypeMap
public class LocationTypeMap : ClassMap<LocationType>
{
public LocationTypeMap()
{
Table("Setting_LocationType");
Id(x => x.LocationTypeId).Column("LocationId").GeneratedBy.Sequence("location_type_seq");
Map(x => x.ShortName, "Name").Length(15).Not.Nullable();
References<LocationType>(x => x.ParentLocationType).LazyLoad().Nullable();
HasMany<LocationType>(x => x.LocationTypes).AsBag().KeyColumn("ParentLocationId").KeyNullable().LazyLoad().Inverse().Cascade.SaveUpdate();
}
}
Jetzt habe ich in dem Abrufen dieser Zeilen ein Problem habe, die NULL enthalten (oder sagen nicht Kind) im Feld PatentLocationType. Ich habe versucht, null wie diese repo.Get("ParentLocationType.LocationTypeId", null);
vorbei, aber es hat nicht funktioniert, aber warf object reference is not set to an instance error.
Abfrage ja, ich habe Es funktionierte auch nicht, aber ich denke, ich weiß warum: Schau dir die sql-Abfrage an, die generiert wird "SELECT LocationId, Name, ParentLocationId FROM Settings_LocationType WHERE ParentLocationId = null". Ich denke, es kann gelöst werden, wenn einige wie ich es machen, um "= null" durch "ist null" zu ersetzen. Irgendeine Idee, wie man das macht? – Waqas
ok, ich löste es mit Expression.IsNull anstelle von Expression.Eq – Waqas