NHibernate的Session.Get和Session.CreateCriteria的区别
-
21-09-2019 - |
题
是什么NHibernate的Session.Get和之间的差Session.CreateCriteria?
我的故事为:
在我们的产品,我们通过将接口ISoftDeletable,其每一个实现该接口具有deletedDate和deletedBy字段类实现的softDeletion。另外,我们有AuditableEntity类这意味着每个类执行它具有:createdDate,createdBy,modifiedDate,modifiedBy
这里的来源:
public class SaveUpdateEventListener : DefaultSaveEventListener
{
private readonly ISecurityContextService securityContextService;
public SaveUpdateEventListener(ISecurityContextService securityContextService)
{
this.securityContextService = securityContextService;
}
protected override object PerformSaveOrUpdate(SaveOrUpdateEvent @event)
{
this.PrepareAuditableEntity(@event);
return base.PerformSaveOrUpdate(@event);
}
private void PrepareAuditableEntity(SaveOrUpdateEvent @event)
{
var entity = @event.Entity as AuditableEntity;
if (entity == null)
{
return;
}
if (this.securityContextService.EdiPrincipal == null)
{
throw new Exception("No logged user.");
}
if (entity.Id == 0)
{
this.ProcessEntityForInsert(entity);
}
else
{
this.ProcessEntityForUpdate(entity);
}
}
private void ProcessEntityForUpdate(AuditableEntity entity)
{
entity.ModifiedBy = securityContextService.GetLoggedUser();
entity.ModifiedDate = DateTime.Now;
}
private void ProcessEntityForInsert(AuditableEntity entity)
{
entity.CreatedBy = securityContextService.GetLoggedUser();
entity.CreatedDate = DateTime.Now;
}
此外,我们已经overrided的Session.Get方法。
public virtual T Get(long id)
{
if (typeof(ISoftDeletable).IsAssignableFrom(typeof(T)))
{
return
this.Session.CreateCriteria(typeof(T))
.Add(Restrictions.Eq("Id", id))
//.Add(Restrictions.IsNull("DeletedDate"))
.UniqueResult<T>();
}
return Session.Get<T>(id);
}
现在在一些上下文中,应用程序在对某softDeletable和可审计实体Get方法抛出的StackOverflow异常。经过一番调查我注意到,它创建PrepareEntityForUpdate / securityContextService.GetLoggedUser之间的循环,并从我们的定制库的方法。 正如你可以看到我已经发表意见的限制DeletedDate,这意味着Session.Get(ID)应返回相同的结果创造条件。但是,如果我去,虽然this.Session.CreateCriteria(typeof运算(T))我得到的StackOverflow例外,如果我评论这一个,只留下返回Session.Get(ID)(不考虑考虑deletiondate)一切工作正常。
这让我想到,Session.Get和Session.CreateCriteria的工作方式不同。 有任何想法吗?
解决方案
获取将使用会话缓存。 标准不会。
在换句话说:标准将总是导致在SQL查询/呼叫到DB。 获取不会总是导致一个SQL查询。如果实体已经在会话中加载由NHibernate的,和你想再次检索实体使用GET,NHibernate的将返回实体,它已经从它的缓存加载。
其他提示
在除了此可以设置在映射类,其中的参数。在那里,你可以添加: “DeletedDate IS NULL”。当执行获取NHibernate的添加此WHERE语句生成的查询。