-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: using an association table in a many-to-many mapping
PostPosted: Thu Jun 29, 2006 3:09 pm 
Newbie

Joined: Thu Jun 29, 2006 2:53 pm
Posts: 3
Hibernate version:1.0.2.0

Mapping documents:
<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="Model.Person, Model" table="Person" lazy="false">
<id name="Id" access="nosetter.camelcase-underscore" column="Id" type="Int32" unsaved-value="0">
<generator class="identity">
</generator>
</id>
<property name="Firstname" access="property" column="Firstname" type="String" />
<property name="Lastname" access="property" column="Lastname" type="String" />
<set name="Organisations" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" table="OrganisationEmployee" lazy="true" inverse="true">
<key column="PersonID" />
<many-to-many class="Model.Organisation, Model" column="OrganisationID" />
</set>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="Model.Organisation, Model" table="Organisation" lazy="false">
<id name="Id" access="nosetter.camelcase-underscore" column="Id" type="Int32" unsaved-value="0">
<generator class="identity">
</generator>
</id>
<property name="Name" access="property" column="Name" type="String" />
<set name="Employees" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" table="OrganisationEmployee" lazy="true">
<key column="OrganisationID" />
<many-to-many class="Model.Person, Model" column="PersonID" />
</set>
</class>
</hibernate-mapping>



<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="Model.OrganisationEmployee, Model" table="OrganisationEmployee" lazy="false">
<id name="Id" access="nosetter.camelcase-underscore" column="Id" type="Int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="Email" access="property" column="Email" type="String" />
<many-to-one name="Person" access="property" class="Model.Person, Model" column="PersonID" />
<many-to-one name="Organisation" access="property" class="Model.Organisation, Model" column="OrganisationID" />
<set name="Appointments" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" table="AppointmentContact" lazy="true" inverse="true">
<key column="OrganisationEmployeeID" />
<many-to-many class="Model.Appointment, Model" column="AppointmentID" />
</set>
</class>
</hibernate-mapping>



<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="Model.Appointment, Model" table="Appointment" lazy="false">
<id name="Id" access="nosetter.camelcase-underscore" column="Id" type="Int32" unsaved-value="0">
<generator class="identity">
</generator>
</id>
<property name="Startdate" access="property" column="Startdate" type="System.DateTime" />
<property name="Enddate" access="property" column="Enddate" type="System.DateTime" />
<set name="Contacts" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics" table="AppointmentContact" lazy="true">
<key column="AppointmentID" />
<many-to-many class="Model.OrganisationEmployee, Model" column="OrganisationEmployeeID" />
</set>
</class>
</hibernate-mapping>




Full stack trace of any exception that occurs:NHibernate.MappingException: Foreign key in table AppointmentContact must have same number of columns as referenced primary key in table OrganisationEmployee
at NHibernate.Mapping.ForeignKey.set_ReferencedTable(Table value)
at NHibernate.Cfg.Configuration.SecondPassCompileForeignKeys(Table table, ISet done)


Name and version of the database you are using:MS SQL Express 2005


Hi, I get this error when i try to use an association table which also has properties of its in a many-to-many relation. I have tried a lot of different mappings but I can't get this to work. Does somebody know how I can solve this issue?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 4:12 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
The many-to-many mappings seem to look OK to me. Are you trying to map an existing database? If you're creating a new database, are you using SchemaExport to generate the database schema?

One thing that I did notice is that for one of the id values, you're using "native" and the other you're using "identity." I don't think that's your problem, but, I'm thinking maybe you should make them the same for sake of consistency.

Are you saying that you have an existing intermediate table that has additional properties that you want to map? In that case, you'd need to create a class for the intermediate class and probably use one-to-manys instead.

Regarding "access="NHibernate.Generics.GenericAccessor, NHibernate.Generics"", I don't think you need that. I think NHibernate (the alpha) automatically knows how to handle generics without it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 2:23 am 
Newbie

Joined: Thu Jun 29, 2006 2:53 pm
Posts: 3
Thanks for your quick response! I don't use schemaexport, but it goes wrong when it tries to validate the mappings, so before it goes to the db. I fixed the different key types, but that's not the cause of the error.

What I am trying to do is this; I have an OrganisationEmployee which is an association table between the many-to-many mapping of Organisation to Employee. But an OrganisationEmployee also has properties of its own, so I have to give it its own mapping.

This all works OK. The problem starts when I have to use OrganisationEmployee in a many-to-many mapping of its own. An appointment can have many OrganisationEmployees as contacts and 1 organisationemployee can have more appointments. When I try to map this I get the error that I described in my first post.

Does this give you more information?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 2:07 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I think I see what you're saying. You've got a table which is the association table in a many-to-many, but, you've also got that table mapped separately so that you can access other properties in it. I've wondered if it was possible to do that. I've never tried it before. Hopefully someone else will chime in with the answer.

What I would probably do at this point is temporarily remove the many-to-many and make sure all the other mappings work OK. Then, do the opposite. This way you can see if the mappings work OK separately and it's the case that they don't work together. Otherwise, maybe there is some other more basic problem.

Sorry I couldn't be of more help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 3:01 pm 
Newbie

Joined: Thu Jun 29, 2006 2:53 pm
Posts: 3
Actually this is possible. Look at the organisationemployee table. It is the association table between organisation and employee. But it also has a property email. This is all working fine. The problem starts when I want to use organisationemployee in a many-to-many mapping with appointment. Then I get the error from my first post.

Anyway, thanks for your interest and your help. I hope somebody else can point me in the right direction.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.