我試圖優化我的查詢,需要一些建議。這是查詢一對多關係的首選方式嗎?
我的領域是這樣的:
MeasureSet
/// <summary>
///
/// </summary>
/// <remarks>
/// Tables: none
/// </remarks>
public class MeasureSet : PersistentEntity
{
#region Properties
/// <summary>
/// Gets or sets the code.
/// </summary>
/// <value>
/// The code.
/// </value>
public virtual string Code { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public virtual string Description { get; set; }
/// <summary>
/// Gets or sets the measure domains.
/// </summary>
/// <value>
/// The measure domains.
/// </value>
public virtual IList<MeasureDomain> MeasureDomains { get; protected set; }
}
MeasureDomain
/// <summary>
///
/// </summary>
/// <remarks>
/// Tables: domeinen
/// </remarks>
public class MeasureDomain : PersistentEntity
{
#region Properties
/// <summary>
/// Gets or sets the code.
/// </summary>
/// <value>
/// The code.
/// </value>
public virtual string Code { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public virtual string Description { get; set; }
/// <summary>
/// Gets or sets the explanation.
/// </summary>
/// <value>
/// The explanation.
/// </value>
public virtual string Explanation { get; set; }
/// <summary>
/// Gets or sets the measure set.
/// </summary>
/// <value>
/// The measure set.
/// </value>
public virtual MeasureSet MeasureSet { get; set; }
/// <summary>
/// Gets or sets the measure sub domains.
/// </summary>
/// <value>
/// The measure sub domains.
/// </value>
public virtual IList<MeasureSubDomain> MeasureSubDomains { get; protected set; }
/// <summary>
/// Gets or sets the audits.
/// </summary>
/// <value>
/// The audits.
/// </value>
public virtual IList<Audit> Audits { get; protected set; }
#endregion
}
MeasureSubDomain
/// <summary>
///
/// </summary>
/// <remarks>
/// Tables: subdomeinen, subdomeinenbestanden
/// </remarks>
public class MeasureSubDomain : PersistentEntity
{
#region Properties
/// <summary>
/// Gets or sets the code.
/// </summary>
/// <value>
/// The code.
/// </value>
public virtual string Code { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public virtual string Description { get; set; }
/// <summary>
/// Gets or sets the domain.
/// </summary>
/// <value>
/// The domain.
/// </value>
public virtual MeasureDomain MeasureDomain { get; set; }
/// <summary>
/// Gets or sets the explanation.
/// </summary>
/// <value>
/// The explanation.
/// </value>
public virtual string Explanation { get; set; }
/// <summary>
/// Gets or sets the files.
/// </summary>
/// <value>
/// The files.
/// </value>
public virtual IList<File> Files { get; protected set; }
/// <summary>
/// Gets or sets the measure controls.
/// </summary>
/// <value>
/// The measure controls.
/// </value>
public virtual IList<MeasureControl> MeasureControls { get; protected set; }
/// <summary>
/// Gets or sets the audits.
/// </summary>
/// <value>
/// The audits.
/// </value>
public virtual IList<Audit> Audits { get; protected set; }
}
我只需要收集子代碼,代碼和描述屬性。目前我使用的查詢是這樣的:
一個一對多查詢
measureSets = LazySessionFactory.CurrentSession.CreateCriteria<MeasureSet>()
.Add(Subqueries.PropertyIn("Id",
DetachedCriteria.For<MeasureDomain>()
.SetProjection(Projections.Property("MeasureSet.Id"))
.Add(Subqueries.PropertyIn("Id",
DetachedCriteria.For<MeasureSubDomain>()
.SetProjection(Projections.Property("MeasureDomain.Id"))
.Add(Subqueries.PropertyIn("Id",
DetachedCriteria.For<MeasureControl>()
.SetProjection(Projections.Property("MeasureSubDomain.Id"))
.Add(Subqueries.PropertyIn("Id",
DetachedCriteria.For<Measure>()
.SetProjection(Projections.Property("MeasureControl.Id"))))))))))
.SetCacheable(true)
.Future<MeasureSet>().ToList();
基類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GRCcontrol.Domain.Entities
{
/// <summary>
/// Base class for domain entities based on NHibernate.
/// </summary>
/// <typeparam name="TId">The type of the id.</typeparam>
public abstract class PersistentEntity<TId> : IEquatable<PersistentEntity<TId>>
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>
/// The id.
/// </value>
public virtual TId Id { get; protected set; }
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return Equals(obj as PersistentEntity<TId>);
}
/// <summary>
/// Determines whether the specified obj is transient.
/// </summary>
/// <param name="obj">The obj.</param>
/// <returns>
/// <c>true</c> if the specified obj is transient; otherwise, <c>false</c>.
/// </returns>
private static bool IsTransient(PersistentEntity<TId> obj)
{
return obj != null &&
Equals(obj.Id, default(TId));
}
/// <summary>
/// Gets the type of the unproxied type, since NHibernate's lazy loading technology, creates proxies from entities.
/// </summary>
/// <returns></returns>
private Type GetUnproxiedType()
{
return GetType();
}
/// <summary>
/// Equalses the specified other.
/// </summary>
/// <param name="other">The other.</param>
/// <returns></returns>
public virtual bool Equals(PersistentEntity<TId> other)
{
if (other == null)
return false;
if (ReferenceEquals(this, other))
return true;
if (!IsTransient(this) &&
!IsTransient(other) &&
Equals(Id, other.Id))
{
var otherType = other.GetUnproxiedType();
var thisType = GetUnproxiedType();
return thisType.IsAssignableFrom(otherType) ||
otherType.IsAssignableFrom(thisType);
}
return false;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
if (Equals(Id, default(TId)))
return base.GetHashCode();
return Id.GetHashCode();
}
}
/// <summary>
/// Base class for domain entities based on NHibernate.
/// </summary>
public abstract class PersistentEntity : PersistentEntity<Guid>
{
}
}
我仍然得到所有屬性,所以還有我目前並不需要的審計等。 由於NHibernate不支持集合上的投影,我想知道我應該如何改變我的代碼以獲得更好的性能。
希望你能幫助我。
你有什麼實際從該查詢想要的嗎?即。您是否需要MeasureSet.Code,MeasureSet.Description和MeasureSet.MeasureDomains?是否僅限於那些擁有子域的控件的測量集? – 2013-02-21 14:31:42
@MartinErnst你的權利。我想要您在評論中說的內容,還需要MeasureDomain.Code,MeasureDomain.Description,MeasureDomain。MeasureSubDomains,然後是MeasureSubDomain.Code等。我不需要對審計,文件等的引用,所以我不想查詢它們。我的查詢也返回這些集合。 – 2013-02-21 14:35:03