I've implemented IsTransient interceptor like this
public interface IIsTransient
{
bool IsTransient {get;}
void OnLoad();
void OnSave();
}
public class IsTransientInterceptor : IInterceptor
{
public bool OnLoad(object entity, object id, object[] state,
string[] propertyNames, IType[] types)
{
if (entity is IIsTransient) ((IIsTransient)entity).OnLoad
();
return false;
}
public bool OnSave(object entity, object id, object[] state, string
[] propertyNames, IType[] types)
{
if (entity is IIsTransient) ((IIsTransient)entity).OnSave
();
return false;
}
public bool? IsTransient(object entity)
{
if (entity is IIsTransient)
{
return ((IIsTransient)entity).IsTransient;
}
return false;
}
... some more code for other stuff
}
and then you entity implements IIsTransient, most likely in a base
class.
#region "IsTransient explicit implementation"
private bool _isTransient = true;
bool IIsTransient.IsTransient
{
get
{
return _isTransient;
}
}
void IIsTransient.OnLoad()
{
_isTransient = false;
}
void IIsTransient.OnSave()
{
_isTransient = false;
}
#endregion
On Apr 1, 11:40 am, James Crowley <[email protected]> wrote:
> Dan,
>
> I hit this a few times working against a legacy database. The solution that
> was easy enough for me was to add a Version column to the db table, which
> nHibernate could then use to differentitate (as Paco suggested). All pretty
> straightforward.
>
> James
>
> 2009/4/1 DannyT <[email protected]>
>
>
>
> > Thanks Paco, that's definitely something I need to spend some more time
> > reading up on.
>
> > For now, the approach we've taken is to simply use an unmapped property
> > which is set on the client application which flags whether it is a new
> > object or not then simply call session.Save(myObject) or
> > session.SaveOrUpdate(myObject) dependant on the flag.
>
> > This feels a little dirty but was very easy to implement and seems to do
> > the job for now. I will definitely look more into the optimistic concurrency
> > you've linked to however as it is very much of interest.
>
> > Thanks for your help on this and if you (or anyone) have any other useful
> > resources i'd be very interested.
>
> > Cheers,
>
> > Dan
>
> > 2009/4/1 Paco Wensveen <[email protected]>
>
> >> You can read about it in the docs here:
>
> >>http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/ma...
> >> and here:
>
> >>http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/tr...
> >> There might be other solutions for your problem, or you might not want
> >> to do this, but a nice side effect of optimistic concurrency with
> >> versioning
> >> is that nhibernate can distinct saved and unsaved entities by the version
> >> number.
> >> On Wed, Apr 1, 2009 at 2:33 PM, DannyT <[email protected]> wrote:
>
> >>> Thanks for that, I've just been looking at this in the docs, could you
> >>> clarify that this means I need to add a version column to my table and a
> >>> Version property to my entity?
>
> >>> 2009/4/1 Paco Wensveen <[email protected]>
>
> >>>> You can use a "version" int property if you also want optimistic
> >>>> concurrency
>
> >>>> On 4/1/09, DannyT <[email protected]> wrote:
>
> >>>>> We're using GUIDs as our identifier, however our entities may be
> >>>>> created on the server (which we rely on NH to sort out the IDs for) or
> >>>>> within a client application which will generate and assign the ID.
>
> >>>>> Currently when trying to save an entity generated from the client app
> >>>>> which will have a GUID but will not already exist in the NH db I get the
> >>>>> error:
>
> >>>>> NHibernate.StaleStateException: Unexpected row count: 0; expected: 1
>
> >>>>> Should NH not detect the entity doesn't already exist in the database,
> >>>>> despite the fact it has an ID and insert it? Otherwise what else should
> >>>>> we
> >>>>> be doing to allow for this?
>
> >>>>> Dan
>
> >>> --
> >>>http://danny-t.co.uk
>
> > --
> >http://danny-t.co.uk
>
> --
> James Crowley
> Managing Director
> Developer Fusion - Connecting developers worldwide
>
> Developer Fusion Ltd | 58 Sandringham Close | Enfield, EN1 3JH
> mob: 07986 624128 web:http://www.developerfusion.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---