Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » Re: Texo: how to declare one-to-many relations for different parent types?
Re: Texo: how to declare one-to-many relations for different parent types? [message #872136] Tue, 15 May 2012 14:16 Go to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Olaf,
This is not possible in relational database, a column can only reference one other table, so not multiple tables. So
these 3 foreign keys need 3 different columns. If class1/2/3 inherit from the same parent class then you could put the
matchSet in that parent class, if it makes functionally sense ofcourse.

Emft has this newsgroup link: news://news.eclipse.org/eclipse.technology.emft
and this forum link: www.eclipse.org/forums/eclipse.technology.emft

gr. Martin

On 05/15/2012 03:59 PM, Olaf Burdziakowski wrote:
> Martin you are right I was not precise enough. My example was about the ABC (MatchSetType). So: again:
>
> Class1 one many MatchSetType Class2 one many MatchSetType Class3 one many MatchSetType
> in side Class1 and Class2 and Class3 there is such part:
>
> @OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
> @OrderColumn(name = "ind")
> @JoinColumns({ @JoinColumn() })
> private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();
>
> The problem is that the table of MatchSetType has foreign key to Class1 AND Class2 AND Class3 set.
>
> I would like to have hthe table of MatchSetType be able to have references to Class1 OR Class2 OR Class3.
>
> How to define this?
> (Where in EMFT is the discussion?)


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: Texo: how to declare one-to-many relations for different parent types? [message #872477 is a reply to message #872136] Wed, 16 May 2012 07:41 Go to previous messageGo to next message
Olaf Burdziakowski is currently offline Olaf BurdziakowskiFriend
Messages: 46
Registered: April 2012
Member
We found the solution:all we need to declare is:

@OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
@JoinTable(
name="MS2ST",
joinColumns = @JoinColumn( name="st_id"),
inverseJoinColumns = @JoinColumn( name="ms_id")
)
@OrderColumn(name = "ind")
private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();

So instead:
@JoinColumns({ @JoinColumn() })

There should be defined:
@JoinTable( name="MS2ST", joinColumns = @JoinColumn( name="st_id"), inverseJoinColumns = @JoinColumn( name="ms_id"))
Re: Texo: how to declare one-to-many relations for different parent types? [message #872502 is a reply to message #872477] Wed, 16 May 2012 08:35 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Olaf,
Hmm, I don't understand yet... afaics the join table now has several columns to match class1/2/3 to the other side. How
many columns does the MS2ST table? And what are their foreign key constraints?

gr. Martin

On 05/16/2012 09:41 AM, Olaf Burdziakowski wrote:
> We found the solution:all we need to declare is:
>
> @OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
> @JoinTable(
> name="MS2ST",
> joinColumns = @JoinColumn( name="st_id"),
> inverseJoinColumns = @JoinColumn( name="ms_id")
> )
> @OrderColumn(name = "ind")
> private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();
>
> So instead:
> @JoinColumns({ @JoinColumn() })
>
> There should be defined:
> @JoinTable( name="MS2ST", joinColumns = @JoinColumn( name="st_id"), inverseJoinColumns = @JoinColumn( name="ms_id"))


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: Texo: how to declare one-to-many relations for different parent types? [message #872619 is a reply to message #872502] Wed, 16 May 2012 13:25 Go to previous messageGo to next message
Olaf Burdziakowski is currently offline Olaf BurdziakowskiFriend
Messages: 46
Registered: April 2012
Member
The solution is according to this explanation: http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/

so there is created an extra table for each relation so for the example there is needed definition in each class:

class Class1 {
@OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
@JoinTable(name="class1_MatchSetType",joinColumns = @JoinColumn( name="c1_id"),inverseJoinColumns = @JoinColumn( name="ms_id"))
@OrderColumn(name = "ind")
private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();

class Class2 {
@OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
@JoinTable(name="class2_MatchSetType",joinColumns = @JoinColumn( name="c2_id"),inverseJoinColumns = @JoinColumn( name="ms_id"))
@OrderColumn(name = "ind")
private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();

class Class3 {
@OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
@JoinTable(name="class3_MatchSetType",joinColumns = @JoinColumn( name="c3_id"),inverseJoinColumns = @JoinColumn( name="ms_id"))
@OrderColumn(name = "ind")
private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();

So at the end there are
3 tables for Class1,Class2,Class3
3 tables for relation Class1_MatchSetType,Class2_MatchSetType,Class3_MatchSetType
1 table with common type - MatchSetType

[Updated on: Wed, 16 May 2012 13:26]

Report message to a moderator

Re: Texo: how to declare one-to-many relations for different parent types? [message #872700 is a reply to message #872619] Wed, 16 May 2012 16:17 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Olaf,
Yes, still I don't yet understand... the reason to have join tables is that you can have duplicates in the one-to-many.
Also there is less change of naming collisions. Why did the direct foreign key association not work? Did you get
collisions in the generated foreign key, I mean did class1/class2/class3 accidentally share the same join column? Btw,
are this containment associations?

Note that in the epackage ORM annotation there are 2 options to control the usage of join tables for containment as well
as non-containment association.

gr. Martiin


On 05/16/2012 03:25 PM, Olaf Burdziakowski wrote:
> The solution is according to this explanation:
> http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/
>
> so there is created an extra table for each relation so for the example there is needed definition in each class:
>
> class Class1 {
> @OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
> @JoinTable(name="class1_MatchSetType",joinColumns = @JoinColumn( name="c1_id"),inverseJoinColumns = @JoinColumn(
> name="ms_id"))
> @OrderColumn(name = "ind")
> private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();
>
> class Class2 {
> @OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
> @JoinTable(name="class2_MatchSetType",joinColumns = @JoinColumn( name="c2_id"),inverseJoinColumns = @JoinColumn(
> name="ms_id"))
> @OrderColumn(name = "ind")
> private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();
>
> class Class3 {
> @OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
> @JoinTable(name="class3_MatchSetType",joinColumns = @JoinColumn( name="c3_id"),inverseJoinColumns = @JoinColumn(
> name="ms_id"))
> @OrderColumn(name = "ind")
> private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();
>
> So at the ent there are
> 3 tables for Class1,Class2,Class3
> 3 tables for relation Class1_MatchSetType,Class2_MatchSetType,Class3_MatchSetType
> 1 table with common type - MatchSetType


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: Texo: how to declare one-to-many relations for different parent types? [message #873104 is a reply to message #872700] Thu, 17 May 2012 13:56 Go to previous messageGo to next message
Olaf Burdziakowski is currently offline Olaf BurdziakowskiFriend
Messages: 46
Registered: April 2012
Member
Maybe I do not understand containment but my case is:

There is a table (MatchSetType) in which there are elements connected to Class1 or Class2 or Class3.
The classes Class1 Class2 Class3 have own tables. So MatchSetType.db_Id is not enough. There must be also defined db_Id of which table: table of Class1 or table of Class2 or table of Class3.

My solution was an extra table for each class: Class1,Class2,Class3.
Do you know a better solution?
Re: Texo: how to declare one-to-many relations for different parent types? [message #873120 is a reply to message #873104] Thu, 17 May 2012 14:18 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
I understand the model is like this:
Class1 --- * MatchSetType
Class2 --- * MatchSetType
Class3 --- * MatchSetType

So a class1 can have zero or more MatchSetType instances, the same for class2 and class3.

In the database this can be modeled using a foreign key column from matchsettype pointing to class1, and another column
pointing to class2 and another column to class3. So each of the class1/2 and 3 will get their own foreign key column in
MatchSetType.

Do you see this when not using join tables? So do you see 3 columns being created in the matchSetType table?

gr. Martin

On 05/17/2012 03:56 PM, Olaf Burdziakowski wrote:
> Maybe I do not understand containment but my case is:
>
> There is a table (MatchSetType) in which there are elements connected to Class1 or Class2 or Class3. The classes Class1
> Class2 Class3 have own tables. So MatchSetType.db_Id is not enough. There must be also defined db_Id of which table:
> table of Class1 or table of Class2 or table of Class3.
>
> My solution was an extra table for each class: Class1,Class2,Class3.
> Do you know a better solution?


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: Texo: how to declare one-to-many relations for different parent types? [message #876428 is a reply to message #873120] Thu, 24 May 2012 15:23 Go to previous messageGo to next message
Olaf Burdziakowski is currently offline Olaf BurdziakowskiFriend
Messages: 46
Registered: April 2012
Member
I understand the idea of separate columns. The point is that Texo generate such declaration:

class1{
@OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
@OrderColumn(name = "ind")
@JoinColumns({ @JoinColumn() })
private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();

and this do not cause separate columns. In database there is only single column for all classes, but keys for many tables.

[Updated on: Thu, 24 May 2012 15:23]

Report message to a moderator

Re: Texo: how to declare one-to-many relations for different parent types? [message #876559 is a reply to message #876428] Thu, 24 May 2012 20:39 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Olaf,
Okay, I am currently reworking this part of Texo. It will work in 2 modes:
- generate complete annotations with full db schema almost guaranteed unique and stable join column naming
- generate minimal annotations, so the ORM defaults apply

In both cases your issue will be solved, the first because each of the class1/2/3 will get a unique column, in the
latter because there won't be a joincolumn annotation generated.

Will be a few days, around the weekend before I publish a new build.

gr. Martin

On 05/24/2012 05:23 PM, Olaf Burdziakowski wrote:
> I understand the idea of separate columns. The point is that Texo generate such declaration:
>
> class1{
> @OneToMany(cascade = { CascadeType.ALL }, targetEntity = MatchSetType.class)
> @OrderColumn(name = "ind")
> @JoinColumns({ @JoinColumn() })
> private List<MatchSetType> matchSet = new ArrayList<MatchSetType>();
>
> and this donot cause seperate columns. In database there is only single column for all clases, but keys for many tables.
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:Problems when running the EMF Store client
Next Topic:Connection between EMFStore server and client
Goto Forum:
  


Current Time: Thu Apr 18 02:47:25 GMT 2024

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

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

Back to the top