Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » @ForeignKey annotation not working in all cases
@ForeignKey annotation not working in all cases [message #991337] Tue, 18 December 2012 14:56 Go to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Martin,

I would be very grateful if you would be able to assist me.

I have a case where I have an eCore that looks 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=&quot;TESTGEN_CASE1&quot;)"/>
    </eAnnotations>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="attribute1" unique="false" upperBound="-1" 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="@JoinTable(name=&quot;TESTGEN_CASE1_ATTRIBUTE1&quot;)&#xA;@Column(name=&quot;ATTRIBUTE1&quot;, length=&quot;400&quot;)&#xA;@ForeignKey(name=&quot;CUSTOM_FOREIGN_KEY&quot;)"/>
      </eAnnotations>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsId">
      <eAnnotations source="teneo.jpa">
        <details key="value" value="@Id&#xA;@Column(name=&quot;BDS_ID&quot;, nullable=&quot;false&quot;)"/>
      </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=&quot;BDS_VERSION&quot;)&#xA;@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>


The point of interest is the @ForeignKey(name="CUSTOM_FOREIGN_KEY") part on attribute1

From what I can see it is not used when generating the hibernate mapping as I would have expected:

<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>
		<list name="attribute1" table="`BDS_1_TESTGEN_CASE1ATTRIBUTE1`" lazy="true" cascade="all,delete-orphan">
			<key update="true">
				<column name="`CASE1_ATTRIBUTE1_BDSID`" not-null="true" unique="false"/>
			</key>
			<list-index column="`CASE1_ATTRIBUTE1_IDX`"/>
			<element type="java.lang.String" not-null="false" unique="false">
				<column not-null="false" unique="false" name="`ATTRIBUTE1`" length="400"/>
			</element>
		</list>
	</class>
</hibernate-mapping>


Which in turn generates a DB script as follows:

create table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] ([CASE1_ATTRIBUTE1_BDSID] numeric(19,0) not null, [ATTRIBUTE1] nvarchar(400) null, [CASE1_ATTRIBUTE1_IDX] int not null, primary key ([CASE1_ATTRIBUTE1_BDSID], [CASE1_ATTRIBUTE1_IDX]))
create table [BDS_1_TESTGEN_CASE1] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION] numeric(19,0) null, primary key ([BDS_ID]))
alter table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] add constraint FKAE50F199422FE191 foreign key ([CASE1_ATTRIBUTE1_BDSID]) references [BDS_1_TESTGEN_CASE1]


Note that the foreign key constraint has an auto-generated value.

I did some testing and if the hibernate mapping from the eCore was generated 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>
		<list name="attribute1" table="`BDS_1_TESTGEN_CASE1ATTRIBUTE1`" lazy="true" cascade="all,delete-orphan">
			<key update="true"  foreign-key="CUSTOM_FOREIGN_KEY">
				<column name="`CASE1_ATTRIBUTE1_BDSID`" not-null="true" unique="false"/>
			</key>
			<list-index column="`CASE1_ATTRIBUTE1_IDX`"/>
			<element type="java.lang.String" not-null="false" unique="false">
				<column not-null="false" unique="false" name="`ATTRIBUTE1`" length="400"/>
			</element>
		</list>
	</class>
</hibernate-mapping>


Note the foreign-key="CUSTOM_FOREIGN_KEY" within the attribute1 list key.

Then it will generate the DB SQL as I was hoping:

create table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] ([CASE1_ATTRIBUTE1_BDSID] numeric(19,0) not null, [ATTRIBUTE1] nvarchar(400) null, [CASE1_ATTRIBUTE1_IDX] int not null, primary key ([CASE1_ATTRIBUTE1_BDSID], [CASE1_ATTRIBUTE1_IDX]))
create table [BDS_1_TESTGEN_CASE1] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION] numeric(19,0) null, primary key ([BDS_ID]))
alter table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] add constraint CUSTOM_FOREIGN_KEY foreign key ([CASE1_ATTRIBUTE1_BDSID]) references [BDS_1_TESTGEN_CASE1]


Do you have any ideas what I might be doing wrong?

Thanks

Rob
Re: @ForeignKey annotation not working in all cases [message #991363 is a reply to message #991337] Tue, 18 December 2012 17:43 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Rob,
The foreign key annotation was not handled correctly for many-attributes. I published a new build, can you retry it?

gr. Martin

On 12/18/2012 03:56 PM, Rob Mising name wrote:
> Hi Martin,
>
> I would be very grateful if you would be able to assist me.
>
> I have a case where I have an eCore that looks 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")"/>
> </eAnnotations>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="attribute1" unique="false" upperBound="-1"
> 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="@JoinTable(name="TESTGEN_CASE1_ATTRIBUTE1")
> @Column(name="ATTRIBUTE1", length="400")
> @ForeignKey(name="CUSTOM_FOREIGN_KEY")"/>
> </eAnnotations>
> </eStructuralFeatures>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="bdsId">
> <eAnnotations source="teneo.jpa">
> <details key="value" value="@Id
> @Column(name="BDS_ID", nullable="false")"/>
> </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>
>
>
> The point of interest is the @ForeignKey(name="CUSTOM_FOREIGN_KEY") part on attribute1
>
> From what I can see it is not used when generating the hibernate mapping as I would have expected:
>
> <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>
> <list name="attribute1" table="`BDS_1_TESTGEN_CASE1ATTRIBUTE1`" lazy="true" cascade="all,delete-orphan">
> <key update="true">
> <column name="`CASE1_ATTRIBUTE1_BDSID`" not-null="true" unique="false"/>
> </key>
> <list-index column="`CASE1_ATTRIBUTE1_IDX`"/>
> <element type="java.lang.String" not-null="false" unique="false">
> <column not-null="false" unique="false" name="`ATTRIBUTE1`" length="400"/>
> </element>
> </list>
> </class>
> </hibernate-mapping>
>
>
> Which in turn generates a DB script as follows:
>
> create table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] ([CASE1_ATTRIBUTE1_BDSID] numeric(19,0) not null, [ATTRIBUTE1]
> nvarchar(400) null, [CASE1_ATTRIBUTE1_IDX] int not null, primary key ([CASE1_ATTRIBUTE1_BDSID], [CASE1_ATTRIBUTE1_IDX]))
> create table [BDS_1_TESTGEN_CASE1] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION]
> numeric(19,0) null, primary key ([BDS_ID]))
> alter table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] add constraint FKAE50F199422FE191 foreign key ([CASE1_ATTRIBUTE1_BDSID])
> references [BDS_1_TESTGEN_CASE1]
>
>
> Note that the foreign key constraint has an auto-generated value.
>
> I did some testing and if the hibernate mapping from the eCore was generated 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>
> <list name="attribute1" table="`BDS_1_TESTGEN_CASE1ATTRIBUTE1`" lazy="true" cascade="all,delete-orphan">
> <key update="true" foreign-key="CUSTOM_FOREIGN_KEY">
> <column name="`CASE1_ATTRIBUTE1_BDSID`" not-null="true" unique="false"/>
> </key>
> <list-index column="`CASE1_ATTRIBUTE1_IDX`"/>
> <element type="java.lang.String" not-null="false" unique="false">
> <column not-null="false" unique="false" name="`ATTRIBUTE1`" length="400"/>
> </element>
> </list>
> </class>
> </hibernate-mapping>
>
>
> Note the foreign-key="CUSTOM_FOREIGN_KEY" within the attribute1 list key.
>
> Then it will generate the DB SQL as I was hoping:
>
> create table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] ([CASE1_ATTRIBUTE1_BDSID] numeric(19,0) not null, [ATTRIBUTE1]
> nvarchar(400) null, [CASE1_ATTRIBUTE1_IDX] int not null, primary key ([CASE1_ATTRIBUTE1_BDSID], [CASE1_ATTRIBUTE1_IDX]))
> create table [BDS_1_TESTGEN_CASE1] ([BDS_ID] numeric(19,0) not null, [DTYPE] nvarchar(255) not null, [BDS_VERSION]
> numeric(19,0) null, primary key ([BDS_ID]))
> alter table [BDS_1_TESTGEN_CASE1ATTRIBUTE1] add constraint CUSTOM_FOREIGN_KEY foreign key ([CASE1_ATTRIBUTE1_BDSID])
> references [BDS_1_TESTGEN_CASE1]
>
>
> Do you have any ideas what I might be doing wrong?
>
> Thanks
>
> Rob
>


--

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: @ForeignKey annotation not working in all cases [message #991367 is a reply to message #991363] Tue, 18 December 2012 18:33 Go to previous messageGo to next message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Martin,

Thanks for the fast reply. I'm stuck on an old version of Teneo at the moment (long story) - please would it be possible to point me at the change you made - then I may be able to apply that to my version and see what happens Smile

Thanks

Rob
Re: @ForeignKey annotation not working in all cases [message #991375 is a reply to message #991367] Tue, 18 December 2012 20:11 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Rob,
This change:
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/commit/?id=6d44c46db3f64cc1f15cc6f72062eaf9b3f0762c

Let me know if it works for you, or not..

gr. Martin

On 12/18/2012 07:33 PM, Rob Mising name wrote:
> Hi Martin,
>
> Thanks for the fast reply. I'm stuck on an old version of Teneo at the moment (long story) - please would it be
> possible to point me at the change you made - then I may be able to apply that to my version and see what happens :)
>
> Thanks
>
> Rob
>


--

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: @ForeignKey annotation not working in all cases [message #991472 is a reply to message #991375] Wed, 19 December 2012 11:25 Go to previous message
Rob Mising name is currently offline Rob Mising nameFriend
Messages: 118
Registered: July 2010
Senior Member
Hi Martin,

Thank you for that - I have managed to patch this locally and test it and it works well!

I have hit another issue that I can't think of a solution for, but thought it best to raise a new post:

http://www.eclipse.org/forums/index.php/m/991470/#msg_991470

If you are able to assist again it would be a grate help.

Thank you for the fast resolution.

Rob
Previous Topic:Handle external changes - determine "changed" resources
Next Topic:[CDO-Net4j] Compression of Net4j/CDO streams
Goto Forum:
  


Current Time: Fri Apr 19 19:23:30 GMT 2024

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

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

Back to the top