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 |
Rob Mising name 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="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
|
|
|
Re: @ForeignKey annotation not working in all cases [message #991363 is a reply to message #991337] |
Tue, 18 December 2012 17:43 |
Martin Taal 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
|
|
| | | |
Goto Forum:
Current Time: Thu Sep 26 13:49:53 GMT 2024
Powered by FUDForum. Page generated in 0.05388 seconds
|