Home » Modeling » EMF » [Teneo] How to specify a Foreign Key name with a one to many relationship
[Teneo] How to specify a Foreign Key name with a one to many relationship [message #991470] |
Wed, 19 December 2012 11:22 |
Rob Mising name Messages: 118 Registered: July 2010 |
Senior Member |
|
|
Hi Martin,
Thank you for the help with my other Foreign Key issue I was seeing - unfortunately, I have hit another problem where I can't work out how to set the Foreign key. I would again be very grateful for your view on this.
If I have an eCore:
<eClassifiers xsi:type="ecore:EClass" name="Case1">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Case1"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isCase" value="true"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Table(name="TESTGEN_CASE1")
@DiscriminatorValue("com.example.testgen.Case1")"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="case2a" lowerBound="1"
eType="#//Case2" eOpposite="#//Case2/case1a">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="case2a"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="CASE2A")
@ForeignKey(name="CASE1_CASE2A")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsId" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//ELongObject">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Id
@Column(name="BDS_ID", nullable="false")
@GeneratedValue(generator="bdsIdGenerator")"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isId" value="true"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsVersion" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//ELongObject">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="BDS_VERSION")
@Version"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isVersion" value="true"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Case2">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Case2"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isCase" value="true"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Table(name="TESTGEN_CASE2")
@DiscriminatorValue("com.example.testgen.Case2")"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="case1a" upperBound="-1"
eType="#//Case1" eOpposite="#//Case1/case2a">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="case1a"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@JoinTable(name="TESTGEN_CASE2_CASE1A")
@Column(name="CASE1A")
@ForeignKey(name="CASE2_CASE1A")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="case2Attr" eType="#//case2Attr_Case2Type">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="case2Attr"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="CASE2ATTR", length="400")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsId" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//ELongObject">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Id
@Column(name="BDS_ID", nullable="false")
@GeneratedValue(generator="bdsIdGenerator")"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isId" value="true"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsVersion" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//ELongObject">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="BDS_VERSION")
@Version"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isVersion" value="true"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
And generate the hibernate mapping, it looks as follows:
<hibernate-mapping auto-import="false">
<class entity-name="testgen.Case1" abstract="false" lazy="false" discriminator-value="com.example.testgen.Case1" table="`BDS_1_TESTGEN_CASE1`">
<meta attribute="eclassName">Case1</meta>
<meta attribute="epackage">http://example.com/testgen</meta>
<id name="bdsId" type="java.lang.Long">
<column not-null="true" unique="false" name="`BDS_ID`"/>
<generator class="com.tibco.bds.core.naming.CaseDataStoreIdGenerator"/>
</id>
<discriminator column="`DTYPE`" type="string"/>
<version name="bdsVersion" type="java.lang.Long">
<column not-null="false" unique="false" name="`BDS_VERSION`"/>
</version>
<many-to-one name="case2a" entity-name="testgen.Case2" lazy="false" foreign-key="FK_CASE1_CASE2A" insert="true" update="true" not-null="false">
<column not-null="false" unique="false" name="`CASE1_CASE2A_BDSID`"/>
</many-to-one>
</class>
<class entity-name="testgen.Case2" abstract="false" lazy="false" discriminator-value="com.example.testgen.Case2" table="`BDS_1_TESTGEN_CASE2`">
<meta attribute="eclassName">Case2</meta>
<meta attribute="epackage">http://example.com/testgen</meta>
<id name="bdsId" type="java.lang.Long">
<column not-null="true" unique="false" name="`BDS_ID`"/>
<generator class="com.tibco.bds.core.naming.CaseDataStoreIdGenerator"/>
</id>
<discriminator column="`DTYPE`" type="string"/>
<version name="bdsVersion" type="java.lang.Long">
<column not-null="false" unique="false" name="`BDS_VERSION`"/>
</version>
<list name="case1a" table="`BDS_1_TESTGEN_CASE2CASE1A`" lazy="true">
<key update="true">
<column name="`CASE2_BDSID`" not-null="true" unique="false"/>
</key>
<list-index column="`CASE2_CASE1A_IDX`"/>
<many-to-many entity-name="testgen.Case1" unique="true" foreign-key="FK_CASE2_CASE1A">
<column name="`CASE1_BDSID`" not-null="true" unique="false"/>
</many-to-many>
</list>
<property name="case2Attr" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
<column not-null="false" unique="false" name="`CASE2ATTR`" length="400"/>
</property>
</class>
</hibernate-mapping>
Which in turn will generage the following SQL:
create table [BDS_1_TESTGEN_CASE1] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION] numeric(19,0) null, [CASE1_CASE2A_BDSID] numeric(19,0) null, primary key ([BDS_ID]))
create table [BDS_1_TESTGEN_CASE2CASE1A] ([CASE2_BDSID] numeric(19,0) not null, [CASE1_BDSID] numeric(19,0) not null, [CASE2_CASE1A_IDX] int not null, primary key ([CASE2_BDSID], [CASE2_CASE1A_IDX]))
create table [BDS_1_TESTGEN_CASE2] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION] numeric(19,0) null, [CASE2ATTR] nvarchar(400) null, primary key ([BDS_ID]))
alter table [BDS_1_TESTGEN_CASE1] add constraint FK_CASE1_CASE2A foreign key ([CASE1_CASE2A_BDSID]) references [BDS_1_TESTGEN_CASE2]
alter table [BDS_1_TESTGEN_CASE2CASE1A] add constraint FK_CASE2_CASE1A foreign key ([CASE1_BDSID]) references [BDS_1_TESTGEN_CASE1]
alter table [BDS_1_TESTGEN_CASE2CASE1A] add constraint FK2DF4D7056C5A4021 foreign key ([CASE2_BDSID]) references [BDS_1_TESTGEN_CASE2]
As you can see I have managed to set the Foreign Keys for two values - but not the FK2DF4D7056C5A4021 value.
If I edit the hibernate mapping to have:
<list name="case1a" table="`BDS_1_TESTGEN_CASE2CASE1A`" lazy="true">
<key update="true" foreign-key="FK_MAN_EDIT">
<column name="`CASE2_BDSID`" not-null="true" unique="false"/>
</key>
<list-index column="`CASE2_CASE1A_IDX`"/>
<many-to-many entity-name="testgen.Case1" unique="true" foreign-key="FK_CASE2_CASE1A">
<column name="`CASE1_BDSID`" not-null="true" unique="false"/>
</many-to-many>
</list>
Then I will get the Foreign Key set. However I can't work out what I would need to annotate in the eCore in order to get it passed to hibernate.
Any guidance you are able to offer would be brilliant.
Thanks
Rob
|
|
|
Re: [Teneo] How to specify a Foreign Key name with a one to many relationship [message #991474 is a reply to message #991470] |
Wed, 19 December 2012 11:56 |
Rob Mising name Messages: 118 Registered: July 2010 |
Senior Member |
|
|
I have done a little bit more testing and other options to work around this. I tried implementing my own version of "TeneoSQLNameStrategy" and overriding the method "getForeignKeyName" - unfortunately this method does not get called for this Foreign Key name (It does appear to be called for the other names if there is no @ForeignKey annotation - but not called on any of the Join table foreign key names!)
So if there was a way of us using this approach - it would also get us around this problem.
Thanks
Rob
[Updated on: Wed, 19 December 2012 12:05] Report message to a moderator
|
|
| |
Re: [Teneo] How to specify a Foreign Key name with a one to many relationship [message #992315 is a reply to message #991886] |
Fri, 21 December 2012 10:33 |
Rob Mising name Messages: 118 Registered: July 2010 |
Senior Member |
|
|
Hi Martin,
Thank you again for sorting that out for me. I have now tried to create a patch against our version of Teneo and it does solve the problem that I was seeing. However these is one more issue that I have found that seems very very similar, I believe it may be related to ManyToMany mappings.
If I have an eCore as follows:
<eClassifiers xsi:type="ecore:EClass" name="Case1">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Case1"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isCase" value="true"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Table(name="TESTGEN_CASE1")
@DiscriminatorValue("com.example.testgen.Case1")"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="attribute1" eType="//attribute1_Case1Type">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="attribute1"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="ATTRIBUTE1", length="400")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EReference" name="case2b" upperBound="-1" eType="//Case2" eOpposite="//Case2/case1a">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="case2b"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@JoinTable(name="TESTGEN_CASE1_CASE2B")
@Column(name="CASE2B")
@ForeignKey(name="CASE1_CASE2B")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsId">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Id
@Column(name="BDS_ID", nullable="false")
@GeneratedValue(generator="bdsIdGenerator")"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isId" value="true"/>
</eAnnotations>
<eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//ELongObject"/>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsVersion">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="BDS_VERSION")
@Version"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isVersion" value="true"/>
</eAnnotations>
<eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//ELongObject"/>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Case2">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Case2"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isCase" value="true"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Table(name="TESTGEN_CASE2")
@DiscriminatorValue("com.example.testgen.Case2")"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="case1a" upperBound="-1" eType="//Case1" eOpposite="//Case1/case2b">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="case1a"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@JoinTable(name="TESTGEN_CASE2_CASE1A")
@Column(name="CASE1A")
@ForeignKey(name="CASE2_CASE1A")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="attribute1" eType="//attribute1_Case2Type">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="attribute1"/>
</eAnnotations>
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="ATTRIBUTE1", length="400")"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsId">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Id
@Column(name="BDS_ID", nullable="false")
@GeneratedValue(generator="bdsIdGenerator")"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isId" value="true"/>
</eAnnotations>
<eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//ELongObject"/>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsVersion">
<eAnnotations source="teneo.jpa">
<details key="value" value="@Column(name="BDS_VERSION")
@Version"/>
</eAnnotations>
<eAnnotations source="bds">
<details key="isVersion" value="true"/>
</eAnnotations>
<eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//ELongObject"/>
</eStructuralFeatures>
</eClassifiers>
It generates the following hibernate mapping:
<hibernate-mapping auto-import="false">
<class entity-name="testgen.Case1" abstract="false" lazy="false" discriminator-value="com.example.testgen.Case1" table="`BDS_2_TESTGEN_CASE1`">
<meta attribute="eclassName">Case1</meta>
<meta attribute="epackage">http://example.com/testgen</meta>
<id name="bdsId" type="java.lang.Long">
<column not-null="true" unique="false" name="`BDS_ID`"/>
<generator class="com.tibco.bds.core.naming.CaseDataStoreIdGenerator"/>
</id>
<discriminator column="`DTYPE`" type="string"/>
<version name="bdsVersion" type="java.lang.Long">
<column not-null="false" unique="false" name="`BDS_VERSION`"/>
</version>
<property name="attribute1" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
<column not-null="false" unique="false" name="`ATTRIBUTE1`" length="400"/>
</property>
<list name="case2b" lazy="true" table="`BDS_2_TESTGEN_CASE1CASE2B`">
<key update="true">
<column name="`CASE1_BDSID`" not-null="true" unique="false"/>
</key>
<list-index column="`CASE1_CASE2B_IDX`"/>
<many-to-many entity-name="testgen.Case2" unique="false" foreign-key="FK_2_TESTGEN_CASE2B">
<column name="`CASE2_BDSID`" not-null="true" unique="false"/>
</many-to-many>
</list>
</class>
<class entity-name="testgen.Case2" abstract="false" lazy="false" discriminator-value="com.example.testgen.Case2" table="`BDS_2_TESTGEN_CASE2`">
<meta attribute="eclassName">Case2</meta>
<meta attribute="epackage">http://example.com/testgen</meta>
<id name="bdsId" type="java.lang.Long">
<column not-null="true" unique="false" name="`BDS_ID`"/>
<generator class="com.tibco.bds.core.naming.CaseDataStoreIdGenerator"/>
</id>
<discriminator column="`DTYPE`" type="string"/>
<version name="bdsVersion" type="java.lang.Long">
<column not-null="false" unique="false" name="`BDS_VERSION`"/>
</version>
<list name="case1a" lazy="true" table="`BDS_2_TESTGEN_CASE2CASE1A`">
<key update="true">
<column name="`CASE2_BDSID`" not-null="true" unique="false"/>
</key>
<list-index column="`CASE2_CASE1A_IDX`"/>
<many-to-many entity-name="testgen.Case1" unique="false" foreign-key="FK_2_TESTGEN_CASE1A">
<column name="`CASE1_BDSID`" not-null="true" unique="false"/>
</many-to-many>
</list>
<property name="attribute1" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
<column not-null="false" unique="false" name="`ATTRIBUTE1`" length="400"/>
</property>
</class>
</hibernate-mapping>
Which then generates the following SQL:
create table [BDS_2_TESTGEN_CASE1CASE2B] ([CASE1_BDSID] numeric(19,0) not null, [CASE2_BDSID] numeric(19,0) not null, [CASE1_CASE2B_IDX] int not null, primary key ([CASE1_BDSID], [CASE1_CASE2B_IDX]))
create table [BDS_2_TESTGEN_CASE1] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION] numeric(19,0) null, [ATTRIBUTE1] nvarchar(400) null, primary key ([BDS_ID]))
create table [BDS_2_TESTGEN_CASE2CASE1A] ([CASE2_BDSID] numeric(19,0) not null, [CASE1_BDSID] numeric(19,0) not null, [CASE2_CASE1A_IDX] int not null, primary key ([CASE2_BDSID], [CASE2_CASE1A_IDX]))
create table [BDS_2_TESTGEN_CASE2] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION] numeric(19,0) null, [ATTRIBUTE1] nvarchar(400) null, primary key ([BDS_ID]))
alter table [BDS_2_TESTGEN_CASE1CASE2B] add constraint FK5A79165377404DF foreign key ([CASE1_BDSID]) references [BDS_2_TESTGEN_CASE1]
alter table [BDS_2_TESTGEN_CASE1CASE2B] add constraint FK_2_TESTGEN_CASE2B foreign key ([CASE2_BDSID]) references [BDS_2_TESTGEN_CASE2]
alter table [BDS_2_TESTGEN_CASE2CASE1A] add constraint FK_2_TESTGEN_CASE1A foreign key ([CASE1_BDSID]) references [BDS_2_TESTGEN_CASE1]
alter table [BDS_2_TESTGEN_CASE2CASE1A] add constraint FK3A8DCC866C5A4021 foreign key ([CASE2_BDSID]) references [BDS_2_TESTGEN_CASE2]
So, I'm wondering if I have made a mess of the patch, or if there is soming else I need to do for Many-to-many support.
Thank you again for all your assistance - and have a great Christmas!
Rob
|
|
| | | |
Goto Forum:
Current Time: Thu Apr 25 22:09:02 GMT 2024
Powered by FUDForum. Page generated in 0.03798 seconds
|