I have finally got it working with a correct implementation of the class in
the composite-id tag. I now have a separate class for City (at the Entity
level) that wraps a CityKey class, which is serializable and overrides
Equals and GetHashCode. I found this on

http://devlicio.us/blogs/anne_epstein/archive/2009/11/20/nhibernate-and-composite-keys.aspx

which was a good sample (in C#).

It is not possible to change two tables in the application as they are
lookups on an enterprise-wide basis with numerous apps all using them (and
all being plagued by a bad initial design that the responsible DBA's are
afraid to fix, because they have no involvement with any application and do
not know what may break!!)

It seems to work ok. thanks for the various inputs

John Davidson

On Sat, Feb 20, 2010 at 10:21 AM, Jason Meckley <[email protected]>wrote:

> can you add a "fake" identity to the table? I have done this in the
> past.
> add an additional field to the table, "fake_pk" or something with an
> auto-incrementing value, just something not-null and unique. Since the
> table is immutable it doesn't really matter. just so long as it's
> there and can look like a PK to NH.
> Then map this new field to NH as the PK and city/providence as a
> natural key and reference the fields of the natural key relating to
> other objects.
>
> The end result is 1 new column on the table in the database which
> serves no other purpose than making NH believe this is an entity. The
> NH mapping has an id linked to this new field and 2 properties which
> compose a natural-id (immutable). You can also make the domain object
> and NH mapping immutable as well.
>
> If you cannot change the table structure, and the database support
> views, create a view with this fake pk column and map NH to the view.
> beyond that you may be able to do something with straight sql to get
> the data you need without mapping it to an entity.
>
> the fact that you are re-designing the application without modifying
> the database severely limits the possibilities and elegance of the
> system. It's unfortunate, but a sad reality of software design even
> today.
>
> On Feb 20, 7:13 am, John Davidson <[email protected]> wrote:
> > Yes, all of Scott Ambler's writings are of value for those that are not
> > frozen in the past. I had actually used some of his writing as reference
> > material when I was reviewing the original application we are now
> re-doing.
> >
> > Some of the DBAs are willing to move into more modern times, but their
> > management is not as they would lose control of the large body of DBA
> staff
> > as they moved out to work with the application staffs. This would result
> in
> > a lower pay grade, so it will not happen unless management one more layer
> up
> > makes it so.
> >
> > This is of course describing a non-functional organization, which means
> they
> > ?require contractors to come in and try to determine ways to achieve
> results
> > despite the organizations shortcomings.
> >
> > John Davidson
> >
> > On Fri, Feb 19, 2010 at 11:01 PM, Fabio Maulo <[email protected]>
> wrote:
> > > That two phrases are part of one of my posts...
> > > This is the full sentiment
> > >http://fabiomaulo.blogspot.com/2009/06/database-eliot-ness-of-it.html
> >
> > > 2010/2/20 John Davidson <[email protected]>
> >
> > > Of course you are correct Fabio. I should be able to redo the
> application
> > >> using a fully modern DB, even working with a proper object model as
> opposed
> > >> to first making a relational model and then fitting an object model to
> the
> > >> data model.
> >
> > >> But I am creating a government enterprise application and must follow
> > >> their standards - relational model to a predetermined standard created
> by
> > >> the DB Architects and must use the DB Architects legacy tables for
> lookup.
> >
> > >> The use of NHibernate was a major departure and that combined with
> doing
> > >> automated Unit and Integration testing pushed the boundaries far
> enough. I
> > >> was not able to get an IoC, so you can see that a modern data design
> > >> methodology was impossible. If I threatened the DBAs who hold absolute
> veto
> > >> power, no progress would be possible.
> >
> > >> Small steps to start and then run and maybe later score.
> >
> > >> Thanks for the sentiments though
> >
> > >> John Davidson
> >
> > >> On Fri, Feb 19, 2010 at 8:44 PM, Fabio Maulo <[email protected]
> >wrote:
> >
> > >>> Why, write a new .NET3.5 application, shouldn’t mean re-think the DB
> ?
> > >>> The DB is not a part of the old application ?
> >
> > >>> We are working in software, is the relational DB a piece of granite
> > >>> technology of the past century and nothing more ?
> >
> > >>> ....again and again... The Eliot Ness of IT still alive
> >
> > >>> 2010/2/19 John Davidson <[email protected]>
> >
> > >>>> The problem is that this is a legacy table (immutable), with more
> than
> > >>>> 5000 rows which need to be displayed to end users as a pick list,
> based on
> > >>>> province. When it gets stored when used by another entity it can be
> mapped
> > >>>> as a value object, but I still need it as a stand alone entity that
> can be
> > >>>> queried. Is there any way to do this without duplicating all the
> values in
> > >>>> another table with an added ID column?
> >
> > >>>> John Davidson
> >
> > >>>> On Fri, Feb 19, 2010 at 3:55 PM, Jason Meckley <
> [email protected]>wrote:
> >
> > >>>>> "I have marked the class 'Serializable' and overridden Equals and
> > >>>>> GetHashCode (although there were no compile error or exceptions
> when I
> > >>>>> did not do this)"
> > >>>>> nor would there be. By marking the object serializable you can
> > >>>>> serialize the configuration. This is useful if you want to save the
> > >>>>> configuration to file and then load it from file, rather than
> building
> > >>>>> the configuration each time.
> > >>>>> Equals and GetHashCode are used by the session to manage the
> identity
> > >>>>> map (1st level cache) not implementing them will not prevent
> > >>>>> complication, but you will get side effects in the session because
> the
> > >>>>> identity map may contain multiple instances of the same object)
> >
> > >>>>> the sql may not be generated because you have no properties
> associated
> > >>>>> wit the entity. if the entity is just city and providence you may
> want
> > >>>>> to consider mapping this as a list of components (value objects)
> > >>>>> rather than an entity.
> >
> > >>>>> On Feb 19, 1:44 pm, John Davidson <[email protected]> wrote:
> > >>>>> > I could really use some help with this as it is taking too long
> todo
> > >>>>> > something I thought was relatively simple.
> >
> > >>>>> > I am unable to get data from a legacy table with a composite key.
> The
> > >>>>> > mapping, class and test query are below. I have marked the class
> > >>>>> > 'Serializable' and overridden Equals and GetHashCode (although
> there
> > >>>>> were no
> > >>>>> > compile error or exceptions when I did not do this). When the
> sample
> > >>>>> query
> > >>>>> > executes there is no sql (or exception) generated.
> >
> > >>>>> > <?xml version="1.0" encoding="utf-8" ?>
> > >>>>> > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> > >>>>> >                    schema="HCOMS_USER"
> > >>>>> >                    assembly="SIS.Model"
> > >>>>> >                    namespace="SIS.Model">
> > >>>>> >   <class name="CityRef" table="CORE_CITY_TOWN">
> > >>>>> >     <composite-id>
> > >>>>> >       <key-property name="CityName" column="CITY_TOWN_NAME_TXT"
> > >>>>> > type="string"/>
> > >>>>> >       <key-property name="ProvinceCD" column="PROVINCE_CD"
> > >>>>> type="string"/>
> > >>>>> >     </composite-id>
> > >>>>> >   </class>
> > >>>>> > </hibernate-mapping>
> >
> > >>>>> > =================================================================
> >
> > >>>>> > <Serializable()> _
> > >>>>> > Public Class CityRef
> > >>>>> >     Implements IAggregateRoot
> >
> > >>>>> >     Private _cityName As String
> > >>>>> >     Private _provinceCd As String
> >
> > >>>>> >     Public Sub New()
> >
> > >>>>> >     End Sub
> >
> > >>>>> >     Public Sub New(ByVal CityName As String, ByVal ProvinceCd As
> > >>>>> String)
> > >>>>> >         _cityName = CityName
> > >>>>> >         _provinceCd = ProvinceCd
> > >>>>> >     End Sub
> >
> > >>>>> >     Public Overridable Property CityName() As String
> > >>>>> >         Get
> > >>>>> >             Return _cityName
> > >>>>> >         End Get
> > >>>>> >         Set(ByVal value As String)
> > >>>>> >             _cityName = value
> > >>>>> >         End Set
> > >>>>> >     End Property
> >
> > >>>>> >     Public Overridable Property ProvinceCD() As String
> > >>>>> >         Get
> > >>>>> >             Return _provinceCd
> > >>>>> >         End Get
> > >>>>> >         Set(ByVal value As String)
> > >>>>> >             _provinceCd = value
> > >>>>> >         End Set
> > >>>>> >     End Property
> >
> > >>>>> >     Public Overrides Function Equals(ByVal obj As Object) As
> Boolean
> > >>>>> >         If obj Is Nothing Then
> > >>>>> >             Return False
> > >>>>> >         End If
> > >>>>> >         Dim t As CityRef = CType(obj, CityRef)
> > >>>>> >         If t Is Nothing Then
> > >>>>> >             Return False
> > >>>>> >         End If
> > >>>>> >         If (Me._cityName = t._cityName AndAlso Me._provinceCd =
> > >>>>> > t._provinceCd) Then
> > >>>>> >             Return True
> > >>>>> >         Else
> > >>>>> >             Return False
> > >>>>> >         End If
> > >>>>> >     End Function
> >
> > >>>>> >     Public Overrides Function GetHashCode() As Integer
> > >>>>> >         Return _cityName.GetHashCode Xor _provinceCd.GetHashCode
> > >>>>> >     End Function
> > >>>>> > End Class
> >
> > >>>>> > ==============================================================
> >
> > >>>>> >         Dim myList As IList(Of CityRef) =  _
> > >>>>> >                            SessionManager.GetCurrentSession _
> > >>>>> >
>  .CreateCriteria(GetType(CityRef)).List(Of
> > >>>>> > CityRef)()
> >
> > >>>>> > John Davidson
> >
> > >>>>> --
> > >>>>> 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]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > >>>>> .
> > >>>>> For more options, visit this group at
> > >>>>>http://groups.google.com/group/nhusers?hl=en.
> >
> > >>>>  --
> > >>>> 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]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > >>>> .
> > >>>> For more options, visit this group at
> > >>>>http://groups.google.com/group/nhusers?hl=en.
> >
> > >>> --
> > >>> Fabio Maulo
> >
> > >>>  --
> > >>> 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]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > >>> .
> > >>> For more options, visit this group at
> > >>>http://groups.google.com/group/nhusers?hl=en.
> >
> > >>  --
> > >> 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]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > >> .
> > >> For more options, visit this group at
> > >>http://groups.google.com/group/nhusers?hl=en.
> >
> > > --
> > > Fabio Maulo
> >
> > >  --
> > > 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]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
>
> --
> 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]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
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.

Reply via email to