Difference between Nhibernate Session.Get and Session.CreateCriteria

What is the difference between Nhibernate Session.Get and Session.CreateCriteria?

My story:

In our product, we implemented softDeletion, adding the ISoftDeletable interface, each class that implements this interface deleted and deleted the field. We also have the AuditableEntity class, which means that every class that implements it has: createdDate, createdBy, modifiedDate, modifiedBy.

Here are the sources:

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;
    }

We also redefined the Session.Get method.

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);
    }

StackOverflow Get softDeletable . , PrepareEntityForUpdate/securityContextService.GetLoggedUser Get . , DeletedDate, , Session.Get(id) , . , .Session.CreateCriteria(typeof (T)) StackOverflow, return Session.Get(id) ( ) .

, Session.Get Session.CreateCriteria -. ?

+3
2

Get . .

: SQL-/ . Get sql-. NHibernate , Get, NHibernate , .

+6

, . : "DeletedDate IS NULL". Get NHibernate, WHERE .

+1

Source: https://habr.com/ru/post/1726780/


All Articles