NHibernate and Criteria not by ID

NHibernate and Criteria not by ID

Old forum URL: forums.lhotka.net/forums/t/4010.aspx


d3vil posted on Sunday, December 09, 2007

hey,
I'm trying to use David Dilworth implemention using NH, but got stuck in a problem.
The implementation uses DataPortal_Fetch(object) to call session.Load(this, identifier); and fill the instance "from inside".

the problem is when I want to load a single object and i don't have it's id (I do have some other info about it). the only way to get a persist instance is to create a new one - and leave the current instance (which dataportal_fetch is currently executing on) - empty.

I can't use BusinessListBase.Get(...) because that will create the object in as child object of the list.

any suggestions?

d3vil

DavidDilworth replied on Wednesday, December 12, 2007

I would suggest that your best choice would be to get the ID of the object.

How can you have other information about an object, but not its unique identifier?

Think objects, not data.

d3vil replied on Wednesday, December 12, 2007

The current structure is unchangeable at the moment...

I have an unique id but its not its PK.

as its seems at the moment i shell load an object the just copy its fields to the object that originally created by the dataPortal.

d3vil

DavidDilworth replied on Thursday, December 13, 2007

I'm sorry but I don't understand Confused [8-)].

You are trying to load an individual Business Object.   And this BO has a unique identifier.  This is perhaps a guid?  So you should be able to write standard CSLA-style code that does this.

Are you saying that the unique identifier (guid) is not the PK on the database table and therefore the NHibernate mapping does not work for you?  And that actually some other column is the PK for that table.

If that is the case, then your Business Object should use the actual PK as the unique identifier instead of a guid.

The whole point of a unique identifier is that "it does what it says on the tin" - it uniquely identifies your BO.  It can be a guid, an int identity, a string.  It doesn't matter as long as it uniquely identifies the BO.

Or am I missing something here??

 

d3vil replied on Thursday, December 13, 2007

well i wish it was that simple :)

but the object has an id (long) that is it's PK (both DB and CSLA)
and i have an option (from business part) to load it according to another id (long) that it is unique in some scenarios.

to make it more visual:
i have an object called foo.
it has a unique id of type long and another column called OnlineFoo which is the Online Id of the Foo object (you can say its a FK).
the "Off-Line" and "Online" foo are the same class object and the off-line has the online Id
so i can get the off-line object using the online Id.

i know its sound very very stupid but this is the situation.

DavidDilworth replied on Friday, December 14, 2007

So could you model it with two different BOs?  It sounds like you have two different Use Cases: 1 for an OnlineFoo and 1 for an OfflineFoo.

By chance they share the same data from the same table, which is fine, but they are used in different places in your application.

Each BO can then have it's own unique identifier marked up as required.  And you would then be able to load the OnlineFoo using the ID from the OfflineFoo.

Would that work?

d3vil replied on Friday, December 14, 2007

well yes, this is how we do it.
but its only working from this direction - load online by offline
i sometimes need to load the offline using the online id (and the online has no reference to the offline id - e.g. offlineFoo(id: 1, online id:2) onlineFoo( id2, online:2) - this is how the system work right now.....


DavidDilworth replied on Friday, December 14, 2007

I think you need to think through your object model. 

You load the Online BO using an Online unique identifier.
You load the Offline BO using an Offline unique identifier.

What you can't do is load an Offline BO directly using an Online unique identifier.  That doesn't really make sense for an individual BO.

Could you perhaps load the Online BO (using an Online unique identifier).  Then get the Offline unique identifier from the Online BO, which would allow you to load the Offline BO?

Copyright (c) Marimer LLC