Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » Problem mapping many-to-many relationship
Problem mapping many-to-many relationship [message #79865] Sat, 14 April 2007 17:27 Go to next message
Jean-Denis Boudreault is currently offline Jean-Denis BoudreaultFriend
Messages: 55
Registered: July 2009
Member
Hi!


i have a little question which i had trouble answering; i would really
appreciate if someone could help me straighten it out. here it goes:

in short, im trying to create a two sided many-to-many relationship. i
have two classes, with two collections pointing to each other. i took my
mapping from here (http://www.elver.org/hibernate/ejb3_examples.html).
this is a pseudo ecore model with annotations:

-----------------------------------------------------

Class A:
|
|---> property collection AtoB
|
|---> Annotation:
@ManyToMany(fetch=LAZY cascade=NONE targetEntity="B" indexed="false")
@JoinTable(name="AandB")


Class B:
|
|---> property collection BtoA
|
|---> Annotation:
@ManyToMany(fetch=LAZY cascade=ALL targetEntity="A" mappedBy="AtoB"
indexed="false")
@JoinTable(name="AandB")

-----------------------------------------------------

from this ecore model, i get the following pseudo mapping file:

-----------------------

<class name="AImpl" entity-name="A" abstract="false" lazy="false"
discriminator-value="A" table="`A`">
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column not-null="false" unique="false" name="`id`"/>
<generator class="identity"/>
</id>
<bag name="AtoB" lazy="true" table="`AandB`">
<key/>
<many-to-many entity-name="B" unique="false"/>
</bag>
</class>

<class name="BImpl" entity-name="Transmitter" abstract="false"
lazy="false" discriminator-value="B" table="`B`">
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column not-null="false" unique="false" name="`id`"/>
<generator class="identity"/>
</id>
<bag name="BtoA" lazy="true" cascade="all" inverse="true" table="`AandB`">
<key/>
<many-to-many entity-name="A" unique="false"/>
</bag>
</class>

-----------------------

now, i notice that there is a <key/> tag with no column on it. is this
normal? and if not( which i suppose), how can i get it to set the column
key. also, how can i set a column in the many-to-many tag to produce
something like this: http://www.xylax.net/hibernate/manytomany.html

in the database, the join table that is created does not have any FK
columns; which obviously creates a runtime error. all it has is two
columns: "id" and "elt" (i dreally dont know where this elt comes from)


how can i properly setup the many-to-many relationship? could it be
that i must use joinColumn or some other annotation? and if so, how must
i use it?


thanks very much for your help!

best regards,
Re: Problem mapping many-to-many relationship [message #79884 is a reply to message #79865] Sat, 14 April 2007 18:53 Go to previous messageGo to next message
Jean-Denis Boudreault is currently offline Jean-Denis BoudreaultFriend
Messages: 55
Registered: July 2009
Member
Hello,

just wanted to say that i managed to find the answer to my question. for
the record, here it is:

i didn't know we could add collections of annotations in annotations. i
actually found it here: http://www.elver.org/hibernate/ejb3_format.html

so, to add a join column to my join table, i added:

@JoinTable(name="AandB" joinColumns={@JoinColumn(name="someiD")})

and gave me a mapping like:

<class name="AImpl" entity-name="A" abstract="false" lazy="false"
discriminator-value="A" table="`A`">
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column not-null="false" unique="false" name="`id`"/>
<generator class="identity"/>
</id>
<bag name="AtoB" lazy="true" table="`AandB`">
<key update="true">
<column name="`someID`" not-null="true" unique="false"/>
</key>
<many-to-many entity-name="B" unique="false"/>
</bag>
</class>

and now all works well.


thanks anyways!!

best regards,


Jean-Denis Boudreault wrote:
> Hi!
>
>
> i have a little question which i had trouble answering; i would really
> appreciate if someone could help me straighten it out. here it goes:
>
> in short, im trying to create a two sided many-to-many relationship. i
> have two classes, with two collections pointing to each other. i took my
> mapping from here (http://www.elver.org/hibernate/ejb3_examples.html).
> this is a pseudo ecore model with annotations:
>
> -----------------------------------------------------
>
> Class A:
> |
> |---> property collection AtoB
> |
> |---> Annotation:
> @ManyToMany(fetch=LAZY cascade=NONE targetEntity="B" indexed="false")
> @JoinTable(name="AandB")
>
>
> Class B:
> |
> |---> property collection BtoA
> |
> |---> Annotation:
> @ManyToMany(fetch=LAZY cascade=ALL targetEntity="A" mappedBy="AtoB"
> indexed="false")
> @JoinTable(name="AandB")
>
> -----------------------------------------------------
>
> from this ecore model, i get the following pseudo mapping file:
>
> -----------------------
>
> <class name="AImpl" entity-name="A" abstract="false" lazy="false"
> discriminator-value="A" table="`A`">
> <cache usage="read-write"/>
> <id name="id" type="java.lang.Integer">
> <column not-null="false" unique="false" name="`id`"/>
> <generator class="identity"/>
> </id>
> <bag name="AtoB" lazy="true" table="`AandB`">
> <key/>
> <many-to-many entity-name="B" unique="false"/>
> </bag>
> </class>
>
> <class name="BImpl" entity-name="Transmitter" abstract="false"
> lazy="false" discriminator-value="B" table="`B`">
> <cache usage="read-write"/>
> <id name="id" type="java.lang.Integer">
> <column not-null="false" unique="false" name="`id`"/>
> <generator class="identity"/>
> </id>
> <bag name="BtoA" lazy="true" cascade="all" inverse="true"
> table="`AandB`">
> <key/>
> <many-to-many entity-name="A" unique="false"/>
> </bag>
> </class>
>
> -----------------------
>
> now, i notice that there is a <key/> tag with no column on it. is this
> normal? and if not( which i suppose), how can i get it to set the column
> key. also, how can i set a column in the many-to-many tag to produce
> something like this: http://www.xylax.net/hibernate/manytomany.html
>
> in the database, the join table that is created does not have any FK
> columns; which obviously creates a runtime error. all it has is two
> columns: "id" and "elt" (i dreally dont know where this elt comes from)
>
>
> how can i properly setup the many-to-many relationship? could it be
> that i must use joinColumn or some other annotation? and if so, how must
> i use it?
>
>
> thanks very much for your help!
>
> best regards,
Re: Problem mapping many-to-many relationship [message #79899 is a reply to message #79884] Sun, 15 April 2007 08:47 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Jean-Denis,
I am not sure why the fk was not created in your first mapping. If you have time can you post the
stack trace?

Yes a key tag without a column attribute is allowed, hibernate will/should then automatically create
the fk columns (which are id and elt).

The elt field is the fk to the element in the mtm and the id is the fk to the parent.

gr. Martin

Jean-Denis Boudreault wrote:
> Hello,
>
> just wanted to say that i managed to find the answer to my question. for
> the record, here it is:
>
> i didn't know we could add collections of annotations in annotations. i
> actually found it here: http://www.elver.org/hibernate/ejb3_format.html
>
> so, to add a join column to my join table, i added:
>
> @JoinTable(name="AandB" joinColumns={@JoinColumn(name="someiD")})
>
> and gave me a mapping like:
>
> <class name="AImpl" entity-name="A" abstract="false" lazy="false"
> discriminator-value="A" table="`A`">
> <cache usage="read-write"/>
> <id name="id" type="java.lang.Integer">
> <column not-null="false" unique="false" name="`id`"/>
> <generator class="identity"/>
> </id>
> <bag name="AtoB" lazy="true" table="`AandB`">
> <key update="true">
> <column name="`someID`" not-null="true" unique="false"/>
> </key>
> <many-to-many entity-name="B" unique="false"/>
> </bag>
> </class>
>
> and now all works well.
>
>
> thanks anyways!!
>
> best regards,
>
>
> Jean-Denis Boudreault wrote:
>> Hi!
>>
>>
>> i have a little question which i had trouble answering; i would really
>> appreciate if someone could help me straighten it out. here it goes:
>>
>> in short, im trying to create a two sided many-to-many relationship. i
>> have two classes, with two collections pointing to each other. i took
>> my mapping from here
>> (http://www.elver.org/hibernate/ejb3_examples.html). this is a pseudo
>> ecore model with annotations:
>>
>> -----------------------------------------------------
>>
>> Class A:
>> |
>> |---> property collection AtoB
>> |
>> |---> Annotation:
>> @ManyToMany(fetch=LAZY cascade=NONE targetEntity="B" indexed="false")
>> @JoinTable(name="AandB")
>>
>>
>> Class B:
>> |
>> |---> property collection BtoA
>> |
>> |---> Annotation:
>> @ManyToMany(fetch=LAZY cascade=ALL targetEntity="A" mappedBy="AtoB"
>> indexed="false")
>> @JoinTable(name="AandB")
>>
>> -----------------------------------------------------
>>
>> from this ecore model, i get the following pseudo mapping file:
>>
>> -----------------------
>>
>> <class name="AImpl" entity-name="A" abstract="false" lazy="false"
>> discriminator-value="A" table="`A`">
>> <cache usage="read-write"/>
>> <id name="id" type="java.lang.Integer">
>> <column not-null="false" unique="false" name="`id`"/>
>> <generator class="identity"/>
>> </id>
>> <bag name="AtoB" lazy="true" table="`AandB`">
>> <key/>
>> <many-to-many entity-name="B" unique="false"/>
>> </bag>
>> </class>
>>
>> <class name="BImpl" entity-name="Transmitter" abstract="false"
>> lazy="false" discriminator-value="B" table="`B`">
>> <cache usage="read-write"/>
>> <id name="id" type="java.lang.Integer">
>> <column not-null="false" unique="false" name="`id`"/>
>> <generator class="identity"/>
>> </id>
>> <bag name="BtoA" lazy="true" cascade="all" inverse="true"
>> table="`AandB`">
>> <key/>
>> <many-to-many entity-name="A" unique="false"/>
>> </bag>
>> </class>
>>
>> -----------------------
>>
>> now, i notice that there is a <key/> tag with no column on it. is this
>> normal? and if not( which i suppose), how can i get it to set the
>> column key. also, how can i set a column in the many-to-many tag to
>> produce something like this:
>> http://www.xylax.net/hibernate/manytomany.html
>>
>> in the database, the join table that is created does not have any FK
>> columns; which obviously creates a runtime error. all it has is two
>> columns: "id" and "elt" (i dreally dont know where this elt comes from)
>>
>>
>> how can i properly setup the many-to-many relationship? could it be
>> that i must use joinColumn or some other annotation? and if so, how
>> must i use it?
>>
>>
>> thanks very much for your help!
>>
>> best regards,


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: Problem mapping many-to-many relationship [message #604751 is a reply to message #79865] Sat, 14 April 2007 18:53 Go to previous message
Jean-Denis Boudreault is currently offline Jean-Denis BoudreaultFriend
Messages: 55
Registered: July 2009
Member
Hello,

just wanted to say that i managed to find the answer to my question. for
the record, here it is:

i didn't know we could add collections of annotations in annotations. i
actually found it here: http://www.elver.org/hibernate/ejb3_format.html

so, to add a join column to my join table, i added:

@JoinTable(name="AandB" joinColumns={@JoinColumn(name="someiD")})

and gave me a mapping like:

<class name="AImpl" entity-name="A" abstract="false" lazy="false"
discriminator-value="A" table="`A`">
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column not-null="false" unique="false" name="`id`"/>
<generator class="identity"/>
</id>
<bag name="AtoB" lazy="true" table="`AandB`">
<key update="true">
<column name="`someID`" not-null="true" unique="false"/>
</key>
<many-to-many entity-name="B" unique="false"/>
</bag>
</class>

and now all works well.


thanks anyways!!

best regards,


Jean-Denis Boudreault wrote:
> Hi!
>
>
> i have a little question which i had trouble answering; i would really
> appreciate if someone could help me straighten it out. here it goes:
>
> in short, im trying to create a two sided many-to-many relationship. i
> have two classes, with two collections pointing to each other. i took my
> mapping from here (http://www.elver.org/hibernate/ejb3_examples.html).
> this is a pseudo ecore model with annotations:
>
> -----------------------------------------------------
>
> Class A:
> |
> |---> property collection AtoB
> |
> |---> Annotation:
> @ManyToMany(fetch=LAZY cascade=NONE targetEntity="B" indexed="false")
> @JoinTable(name="AandB")
>
>
> Class B:
> |
> |---> property collection BtoA
> |
> |---> Annotation:
> @ManyToMany(fetch=LAZY cascade=ALL targetEntity="A" mappedBy="AtoB"
> indexed="false")
> @JoinTable(name="AandB")
>
> -----------------------------------------------------
>
> from this ecore model, i get the following pseudo mapping file:
>
> -----------------------
>
> <class name="AImpl" entity-name="A" abstract="false" lazy="false"
> discriminator-value="A" table="`A`">
> <cache usage="read-write"/>
> <id name="id" type="java.lang.Integer">
> <column not-null="false" unique="false" name="`id`"/>
> <generator class="identity"/>
> </id>
> <bag name="AtoB" lazy="true" table="`AandB`">
> <key/>
> <many-to-many entity-name="B" unique="false"/>
> </bag>
> </class>
>
> <class name="BImpl" entity-name="Transmitter" abstract="false"
> lazy="false" discriminator-value="B" table="`B`">
> <cache usage="read-write"/>
> <id name="id" type="java.lang.Integer">
> <column not-null="false" unique="false" name="`id`"/>
> <generator class="identity"/>
> </id>
> <bag name="BtoA" lazy="true" cascade="all" inverse="true"
> table="`AandB`">
> <key/>
> <many-to-many entity-name="A" unique="false"/>
> </bag>
> </class>
>
> -----------------------
>
> now, i notice that there is a <key/> tag with no column on it. is this
> normal? and if not( which i suppose), how can i get it to set the column
> key. also, how can i set a column in the many-to-many tag to produce
> something like this: http://www.xylax.net/hibernate/manytomany.html
>
> in the database, the join table that is created does not have any FK
> columns; which obviously creates a runtime error. all it has is two
> columns: "id" and "elt" (i dreally dont know where this elt comes from)
>
>
> how can i properly setup the many-to-many relationship? could it be
> that i must use joinColumn or some other annotation? and if so, how must
> i use it?
>
>
> thanks very much for your help!
>
> best regards,
Re: Problem mapping many-to-many relationship [message #604754 is a reply to message #79884] Sun, 15 April 2007 08:47 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Jean-Denis,
I am not sure why the fk was not created in your first mapping. If you have time can you post the
stack trace?

Yes a key tag without a column attribute is allowed, hibernate will/should then automatically create
the fk columns (which are id and elt).

The elt field is the fk to the element in the mtm and the id is the fk to the parent.

gr. Martin

Jean-Denis Boudreault wrote:
> Hello,
>
> just wanted to say that i managed to find the answer to my question. for
> the record, here it is:
>
> i didn't know we could add collections of annotations in annotations. i
> actually found it here: http://www.elver.org/hibernate/ejb3_format.html
>
> so, to add a join column to my join table, i added:
>
> @JoinTable(name="AandB" joinColumns={@JoinColumn(name="someiD")})
>
> and gave me a mapping like:
>
> <class name="AImpl" entity-name="A" abstract="false" lazy="false"
> discriminator-value="A" table="`A`">
> <cache usage="read-write"/>
> <id name="id" type="java.lang.Integer">
> <column not-null="false" unique="false" name="`id`"/>
> <generator class="identity"/>
> </id>
> <bag name="AtoB" lazy="true" table="`AandB`">
> <key update="true">
> <column name="`someID`" not-null="true" unique="false"/>
> </key>
> <many-to-many entity-name="B" unique="false"/>
> </bag>
> </class>
>
> and now all works well.
>
>
> thanks anyways!!
>
> best regards,
>
>
> Jean-Denis Boudreault wrote:
>> Hi!
>>
>>
>> i have a little question which i had trouble answering; i would really
>> appreciate if someone could help me straighten it out. here it goes:
>>
>> in short, im trying to create a two sided many-to-many relationship. i
>> have two classes, with two collections pointing to each other. i took
>> my mapping from here
>> (http://www.elver.org/hibernate/ejb3_examples.html). this is a pseudo
>> ecore model with annotations:
>>
>> -----------------------------------------------------
>>
>> Class A:
>> |
>> |---> property collection AtoB
>> |
>> |---> Annotation:
>> @ManyToMany(fetch=LAZY cascade=NONE targetEntity="B" indexed="false")
>> @JoinTable(name="AandB")
>>
>>
>> Class B:
>> |
>> |---> property collection BtoA
>> |
>> |---> Annotation:
>> @ManyToMany(fetch=LAZY cascade=ALL targetEntity="A" mappedBy="AtoB"
>> indexed="false")
>> @JoinTable(name="AandB")
>>
>> -----------------------------------------------------
>>
>> from this ecore model, i get the following pseudo mapping file:
>>
>> -----------------------
>>
>> <class name="AImpl" entity-name="A" abstract="false" lazy="false"
>> discriminator-value="A" table="`A`">
>> <cache usage="read-write"/>
>> <id name="id" type="java.lang.Integer">
>> <column not-null="false" unique="false" name="`id`"/>
>> <generator class="identity"/>
>> </id>
>> <bag name="AtoB" lazy="true" table="`AandB`">
>> <key/>
>> <many-to-many entity-name="B" unique="false"/>
>> </bag>
>> </class>
>>
>> <class name="BImpl" entity-name="Transmitter" abstract="false"
>> lazy="false" discriminator-value="B" table="`B`">
>> <cache usage="read-write"/>
>> <id name="id" type="java.lang.Integer">
>> <column not-null="false" unique="false" name="`id`"/>
>> <generator class="identity"/>
>> </id>
>> <bag name="BtoA" lazy="true" cascade="all" inverse="true"
>> table="`AandB`">
>> <key/>
>> <many-to-many entity-name="A" unique="false"/>
>> </bag>
>> </class>
>>
>> -----------------------
>>
>> now, i notice that there is a <key/> tag with no column on it. is this
>> normal? and if not( which i suppose), how can i get it to set the
>> column key. also, how can i set a column in the many-to-many tag to
>> produce something like this:
>> http://www.xylax.net/hibernate/manytomany.html
>>
>> in the database, the join table that is created does not have any FK
>> columns; which obviously creates a runtime error. all it has is two
>> columns: "id" and "elt" (i dreally dont know where this elt comes from)
>>
>>
>> how can i properly setup the many-to-many relationship? could it be
>> that i must use joinColumn or some other annotation? and if so, how
>> must i use it?
>>
>>
>> thanks very much for your help!
>>
>> best regards,


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Previous Topic:Problem mapping many-to-many relationship
Next Topic:ClassCastExeption with Elistwrapper
Goto Forum:
  


Current Time: Tue Oct 20 12:03:33 GMT 2020

Powered by FUDForum. Page generated in 0.02252 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top