Validator expected exceptions

35 views
Skip to first unread message

ESwann

unread,
Jan 14, 2011, 10:02:12 AM1/14/11
to NHibernate Contrib - Development Group
Pretty basic questions on the validator which I wasn't able to find on
the wiki:

When I hook up the validators, I expected to get
InvalidStateException. Instead I am seeing PropertyValueException.
This is better than the app attempting to save, but I was hoping to
get a collection of all invalid properties instead of one sequential
exception on each property. This also appears to deviate from the
expected behavior that is explained in the wiki.

I am using NHib 3.0 with the validator Beta 1. I'm configuring
Validation at app startup and relying on the autoregister listeners to
perform validation on insert/update.

Is this the expected behavior or am I just doing something
incorrectly?


Thanks!

Dario Quintana

unread,
Jan 14, 2011, 10:13:17 AM1/14/11
to nhc...@googlegroups.com
Hi,

What is the code that gets that exception ? 

BTW, you've examples with NH + NHV in the zip with examples & src. Have a look, IIRC is a console application.

Cheers


--
You received this message because you are subscribed to the Google Groups "NHibernate Contrib - Development Group" group.
To post to this group, send email to nhc...@googlegroups.com.
To unsubscribe from this group, send email to nhcdevs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhcdevs?hl=en.




--
Dario Quintana

Valeriu Caraulean

unread,
May 2, 2011, 6:08:12 AM5/2/11
to nhc...@googlegroups.com

Hello

I've hit same case lately. With "non null and not empty" constraint validation isn't even getting involved, NHibernate is throwing
PropertyValueException on Save.
Stack trace:  
at NHibernate.Engine.Nullability.CheckNullability(Object[] values, IEntityPersister persister, Boolean isUpdate)
at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at NHibernate.Validator.Tests.Integration.UniquenessValidationUsingExternalValidatorFixture.Should_throw_InvalidStateException() in ValidatingProxyFixture.cs: line 292

Bellow is the code showing the problem. I would be creating a JIRA ticket, it looks like a bug for me. Or is it not the case?

A fixture that reproduces the problem (sorry for formatting, paste into Integration folder in tests ):

[TestFixture]
public class UniquenessValidationUsingExternalValidatorFixture : PersistenceTest
{
protected override void Configure(NHibernate.Cfg.Configuration configuration)
{
base.Configure(configuration);

var validatorConf = new FluentConfiguration();
validatorConf
.SetDefaultValidatorMode(ValidatorMode.UseExternal)
.IntegrateWithNHibernate
.ApplyingDDLConstraints()
.RegisteringListeners();

var vDefSimple = new ValidationDef<Relation>();
vDefSimple.Define(s => s.Description).NotNullableAndNotEmpty();
validatorConf.Register(vDefSimple);

var engine = new ValidatorEngine();
engine.Configure(validatorConf);
ValidatorInitializer.Initialize(configuration, engine);
}

[Test]
[ExpectedException(typeof(InvalidStateException))]
public void Should_throw_InvalidStateException()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.Save(new Relation());
tx.Commit();
}

CleanDb();
}

private void CleanDb()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from Relation").ExecuteUpdate();
tx.Commit();
}
}

protected override IList Mappings
{
get { return new[] {"Integration.SimpleWithRelation.hbm.xml"}; }
}
}


Valeriu Caraulean

unread,
May 2, 2011, 8:26:49 AM5/2/11
to nhc...@googlegroups.com

What's is extremely strange for me, the field I'm validating doesn't have "not-null=true" set. So, NHibernate's should let the entity to be saved.


Mapping for Relation:
<class name="Relation">
   <id name="Id">
     <generator class="native"/>
   </id>
    <property name="Description"/>
</class>


Valeriu Caraulean

unread,
May 2, 2011, 8:49:33 AM5/2/11
to nhc...@googlegroups.com

OK, I got confused a bit, I forgot how NHValidator works.

So, what's happening is that NHibernate.Validator is adding " not null" constraint to configuration. Then NHibernate tries to Save an entity and checks for not null value and throwing. My bad, forgot this detail...

Are there any other way to get "NotNull" validation without disabling DDL constraints?

Fabio Maulo

unread,
May 2, 2011, 11:57:02 AM5/2/11
to nhc...@googlegroups.com
Yes, the "satisfactor".

--
You received this message because you are subscribed to the Google Groups "NHibernate Contrib - Development Group" group.
To post to this group, send email to nhc...@googlegroups.com.
To unsubscribe from this group, send email to nhcdevs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhcdevs?hl=en.



--
Fabio Maulo

Reply all
Reply to author
Forward
0 new messages