Composite ID against Oracle 10g table

paul_brower's Avatar

paul_brower

22 Sep, 2010 03:04 PM via web

I have a table called ANIMALS, which has a composite key consisting of the following two varchar columns: ANIMAL_ID_TYPE_ID, ANIMAL_ID

The following class map works if I 'cheat' and comment out the CompositeId line, and uncomment the Id(x=>x.ANIMAL_ID) line.

    public AAGDWAnimalMap()
    {
        Table("ANIMALS");
        //Id(x => x.ANIMAL_ID);
        CompositeId().KeyProperty(x => x.ANIMAL_ID_TYPE_ID).KeyProperty(y => y.ANIMAL_ID);
        Map(x => x.ANIMAL_ID_TYPE_ID);
        Map(x => x.ANIMAL_ID);
        Map(x => x.LINE_ID);
    }

I receive the following error when trying to use this class with the CompositeId line set: I've googled for 4 hours and not been able to figure out why this is failing.

TestCase 'Newsham.Fluent.Data.Test.Test1.testOracleAnimals'
failed: FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

----> NHibernate.MappingException : Could not compile the mapping document: (XmlDocument) ----> NHibernate.MappingException : composite-id class must override Equals(): Newsham.Fluent.Data.DataObjects.AAGDWAnimal

d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs(119,0): at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
OracleSessionManager.cs(33,0): at Newsham.Fluent.Data.OracleSessionManager.Initialize()
Class1.cs(18,0): at Newsham.Fluent.Data.Test.Test1.testOracleAnimals()
--FluentConfigurationException
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc)
d:\Builds\FluentNH\src\FluentNHibernate\PersistenceModel.cs(262,0): at FluentNHibernate.PersistenceModel.Configure(Configuration cfg)
d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentMappingsContainer.cs(140,0): at FluentNHibernate.Cfg.FluentMappingsContainer.Apply(Configuration cfg)
d:\Builds\FluentNH\src\FluentNHibernate\Cfg\MappingConfiguration.cs(56,0): at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg)
d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs(110,0): at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
--MappingException
at NHibernate.Cfg.XmlHbmBinding.ClassCompositeIdBinder.BindCompositeId(HbmCompositeId idSchema, PersistentClass rootClass)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
  1. Support Staff 2 Posted by Paul Batum on 22 Sep, 2010 09:47 PM

    Paul Batum's Avatar

    "composite-id class must override Equals()"

    Sounds like one of the properties you are mapping in your composite key is
    not of a type that overrides Equals. What is the type for your ANIMAL_ID
    property and your ANIMAL_ID_TYPE_ID property?

    On Wed, Sep 22, 2010 at 10:10 AM, Paul Brower <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  2. 3 Posted by paul_brower on 22 Sep, 2010 10:06 PM

    paul_brower's Avatar

    Varchar/string. Both of them

  3. Support Staff 4 Posted by Paul Batum on 23 Sep, 2010 12:04 AM

    Paul Batum's Avatar

    That seems odd. You should export your mapping to xml so we can double check
    the types.

    http://wiki.fluentnhibernate.org/Fluent_configuration#Exporting_mappings

    On Wed, Sep 22, 2010 at 5:08 PM, Paul Brower <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

  4. 5 Posted by paul_brower on 23 Sep, 2010 12:42 PM

    paul_brower's Avatar

    The following is the mapping created from the AAGDWAnimal class:

    • <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
    • <composite-id mapped="false" unsaved-value="undefined">
    • <key-property name="ANIMAL_ID_TYPE_ID" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> </key-property>
    • <key-property name="ANIMAL_ID" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> </key-property> </composite-id>
    • </hibernate-mapping>
  5. 6 Posted by paul_brower on 23 Sep, 2010 12:43 PM

    paul_brower's Avatar

    Didn't quite paste nicely. I've attached the mapping file.

  6. 7 Posted by paul_brower on 23 Sep, 2010 01:09 PM

    paul_brower's Avatar

    Did a little more googling, and found some people with similar type of issues. If I override the GetHashCode and Equals methods on my data class, the error goes away.

    So, I added the following to the AAGDWAnimal class, and it now works. I guess my question is, do I need to include all my fields in the GetHashCode, or can I just include the fields that comprise the composite key?

        public override int GetHashCode()
        {
            int hashCode = 0;
            hashCode = hashCode ^ AnimalIdTypeId.GetHashCode() ^ AnimalId.GetHashCode();
            return hashCode;
        }
    
        public override bool Equals(object obj)
        {
            var toCompare = obj as AAGDWAnimal;
            if (toCompare == null) return false;
    
            return (this.GetHashCode() != toCompare.GetHashCode());
        }
  7. Support Staff 8 Posted by Paul Batum on 25 Sep, 2010 10:25 PM

    Paul Batum's Avatar

    Ohhhh so a class that HAS a composite key has to override those methods.
    That error from NH could be a little clearer! (Lucky me, I've never had to
    use composite ID's)

    The best way to implement GetHashCode is outside the scope of this list. I
    expect its the sort of thing you can get a good answer for just by googling,
    or you could ask on the nhibernate
    users<http://groups.google.com/group/nhusers/> mailing
    list.

    On Thu, Sep 23, 2010 at 8:11 AM, Paul Brower <
    ***@tenderapp.com<tender%***@tenderapp.com>
    > wrote:

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What day comes after Monday?