Composite ID against Oracle 10g table
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)
Support Staff 2 Posted by Paul Batum on 22 Sep, 2010 09:47 PM
"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:
3 Posted by paul_brower on 22 Sep, 2010 10:06 PM
Varchar/string. Both of them
Support Staff 4 Posted by Paul Batum on 23 Sep, 2010 12:04 AM
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:
5 Posted by paul_brower on 23 Sep, 2010 12:42 PM
The following is the mapping created from the AAGDWAnimal class:
6 Posted by paul_brower on 23 Sep, 2010 12:43 PM
Didn't quite paste nicely. I've attached the mapping file.
7 Posted by paul_brower on 23 Sep, 2010 01:09 PM
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?
Support Staff 8 Posted by Paul Batum on 25 Sep, 2010 10:25 PM
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: