Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6191743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:49:12+00:00 2026-05-24T02:49:12+00:00

I’ve setup nhibernate but I have problems with validating entity before the actual save

  • 0

I’ve setup nhibernate but I have problems with validating entity before the actual save and commit to database. As I load and change persistent entity, validation that goes after is accessing the database and by doing so commiting the transaction, thus saving entity in database before the actual validation result is known.

I use Repository pattern in .NET MVC web app and it goes like this:

public ActionResult Edit(int id, Entity entity)
{
    var repository = new EntityRepository();

    // get persistent entity from nhibernate
    var entityOriginal = repository.GetById(id);

    // copy data from input entity to persistent entity
    entity.CopyDataToEntity(entityOriginal);

    // if validation ok save to database
    // this validations is problematic becouse it needs info from database and uses transaction
    ValidationResult validationResult = entityOriginal.Validate();
    if (validationResult.IsValid())
    {
        repository.Save(entityOriginal);

        // everything ok, redirect to view entity page
        return RedirectToAction("view");
    }
    else
    {
        repository.Evict(prateciList);
        foreach (var message in validationResult.ErrorMessages)
        {
            ModelState.AddModelError(message.Key, message.Value);
        }
        return RedirectToAction("pogledaj");
    }
}

UPDATE: clarification
ok here’s the code:

// get persistent entity from nhibernate
var entityOriginal = repository.GetById(id);
entity.CopyDataToEntity(entityOriginal);
ValidationResult validationResult = entityOriginal.Validate();
  1. I get the persistent entity from the database
  2. I change it… now if session is flushed it will be persisted in database
  3. I do entity.Validate() which gets another entity from database and commits in process (as you have to commit even for fetching from database)!… so entity I try to validate is persisted in database during the validation itself.

So the problem is that Entity.Validate() check the database for correctly inputed data, and since transaction is commited even on session.Get(), changed entity is saved (valid or invalid) right there.

My repository implementation is classic:

public virtual bool Save(T entity)
    {
        var session = SessionProvider.GetRequestSession(this.DatabaseName);
        using (var transaction = session.BeginTransaction())
        {
            try
            {
                session.SaveOrUpdate(entity);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                _log.Error("Could not save object to database.", ex);
                if (transaction.IsActive)
                {
                    transaction.Rollback();

                }
                session.Close();
                return false;
            }
        }

        return true;
    }

How can I achieve validation that can check stuff in database?

Possible solution be to validate before I make any changes to persistent object, but this is such a pain to do there must be a better way. Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:49:13+00:00Added an answer on May 24, 2026 at 2:49 am

    The solution I’ve found is very ugly but it works:

    1. get original entity from database
    2. clone it (copy every data) to new instance
    3. modify and validate new instance
    4. if validation ok, copy everything from new instance to original and save

    Still looking for a better solution though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.