Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » foreign key problems saving an EMF model
foreign key problems saving an EMF model [message #1282848] Wed, 02 April 2014 21:11 Go to next message
Conchi Sanz is currently offline Conchi SanzFriend
Messages: 10
Registered: December 2013
Junior Member
Hello,
I'm using Teneo/Hibernate to save in a legacy database an object Root which follows a metamodel like this:

class Root
List A;
List B;
/*Root is a container for elements A and B*/

class A
int ID
int idParent
int idChild

class B
int ID
string name


In the database, the tables where these classes are mapping are basically as follow:

table Root
int ID not null generated by default as identity //primary key

Table A
int ID not null generated by default as identity //primary key
int idParent
int idChild
int rootId
foreign key idParent references B(ID)
foreign key idChild references B(ID)
foreign key rootId references root(ID)

Table B
int ID not null generated by default as identity //primary key
varchar name
int rootId
foreign key rootId references B(ID)


When I use a single session.save to save in the database an entire object Root containing several objects A and B, I get an SQL ERROR in the session.getTransaction.commit operation:
"SQLCODE=-530, SQLSTATE=23503 THE INSERT OR UPDATE VALUE OF FOREIGN KEY constraint-name IS INVALID"
because the commit is first done for A objects and there is no B elements committed yet.
What can I do to get it right with a single save operation instead of listing objects A and B one by one and saving them in the right order?
Is there any configuration option to use in Teneo/Hibernate? Something to add in the mapping file?

Regards.
Re: foreign key problems saving an EMF model [message #1283066 is a reply to message #1282848] Thu, 03 April 2014 03:04 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Conchi,
Probably setting/trying different cascade settings in the associations will help here. Can you post/email your ecore
model, to see how you set the model up?

Btw, as you have the model at hand at runtime it is possible to write generic code which can force specific
insert/update behavior. But let's first look at cascade settings in the model itself.

gr. Martin

On 04/02/2014 11:11 PM, Conchi Sanz wrote:
> Hello,
> I'm using Teneo/Hibernate to save in a legacy database an object Root which follows a metamodel like this:
> class Root
> List A;
> List B;
> /*Root is a container for elements A and B*/ class A
> int ID
> int idParent int idChild
> class B
> int ID
> string name
>
>
> In the database, the tables where these classes are mapping are basically as follow:
>
> table Root
> int ID not null generated by default as identity //primary key
> Table A
> int ID not null generated by default as identity //primary key
> int idParent int idChild
> int rootId
> foreign key idParent references B(ID) foreign key idChild references B(ID)
> foreign key rootId references root(ID)
> Table B
> int ID not null generated by default as identity //primary key
> varchar name
> int rootId
> foreign key rootId references B(ID)
> When I use a single session.save to save in the database an entire object Root containing several objects A and B, I get
> an SQL ERROR in the session.getTransaction.commit operation: "SQLCODE=-530, SQLSTATE=23503 THE INSERT OR UPDATE VALUE OF
> FOREIGN KEY constraint-name IS INVALID"
> because the commit is first done for A objects and there is no B elements committed yet. What can I do to get it right
> with a single save operation instead of listing objects A and B one by one and saving them in the right order?
> Is there any configuration option to use in Teneo/Hibernate? Something to add in the mapping file?
> Regards.


--

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: foreign key problems saving an EMF model [message #1283275 is a reply to message #1283066] Thu, 03 April 2014 08:27 Go to previous messageGo to next message
Conchi Sanz is currently offline Conchi SanzFriend
Messages: 10
Registered: December 2013
Junior Member

Hello Martin,

I have uploaded a basic ecore with the characteristics of the one I am using.
There is no more associations in the ecore than the ones existing in the root class. Maybe it is just a matter of reordering the associations in Root class in the proper way. I have supposed that the saving order is alphabethical by default but maybe it just follows the order set in the ecore.
Anyhow, any comment will be more than welcome.

Regards.
  • Attachment: model.ecore
    (Size: 386.04KB, Downloaded 187 times)
Re: foreign key problems saving an EMF model [message #1283533 is a reply to message #1283066] Thu, 03 April 2014 14:27 Go to previous messageGo to next message
Conchi Sanz is currently offline Conchi SanzFriend
Messages: 10
Registered: December 2013
Junior Member

Hello Martin,

I have solved the problem sorting the associations existing in the Root class at the mapping file (hibernate.hbm.xml). It is the mapping file the one who seems to force the order at commit.
So, in my example, it would be just changing

<class Root>
<List name="A"> ... </List>
<List name="B"> ... </List>
</class>

by

<class Root>
<List name="B"> ... </List>
<List name="A"> ... </List>
</class>

The ecore does not need to be modifyed in this case, just the mapping file.

If there is any other alternative, probably a more formal one, I would be glad to know it.
Thanks for your time.
Regards,
Conchi Sanz
Re: foreign key problems saving an EMF model [message #1283582 is a reply to message #1283533] Thu, 03 April 2014 15:33 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Conchi,
Yes maybe there is, manually changing the mapping file is a pain when the model/annotations change, so it is slightly
better I think to control this through annotations in the model.
If you share/email the model then I can comment in some more detail.

gr. Martin

On 04/03/2014 04:27 PM, Conchi Sanz wrote:
>
> Hello Martin,
>
> I have solved the problem sorting the associations existing in the Root class at the mapping file (hibernate.hbm.xml).
> It is the mapping file the one who seems to force the order at commit.
> So, in my example, it would be just changing
>
> <class Root>
> <List name="A"> ... </List>
> <List name="B"> ... </List>
> </class>
>
> by
> <class Root>
> <List name="B"> ... </List>
> <List name="A"> ... </List>
> </class>
>
> The ecore does not need to be modifyed in this case, just the mapping file.
>
> If there is any other alternative, probably a more formal one, I would be glad to know it.
> Thanks for your time.
> Regards,
> Conchi Sanz


--

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: foreign key problems saving an EMF model [message #1284348 is a reply to message #1283582] Fri, 04 April 2014 11:18 Go to previous messageGo to next message
Conchi Sanz is currently offline Conchi SanzFriend
Messages: 10
Registered: December 2013
Junior Member

Hi Martin,
sorry for the ping-pong effect. The message sent on Thu, 03 April 2014 04:27 included a basic ecore model. If it is not enough I will send you a larger one, but will not contain new information, just more clases and more dependencies among them. Any other class has association relations apart from Root.

You are right, if something changes in the ecore, the mapping will also have to change. In that case what I would do it is reordering the associations in the ecore itself and derived the mapping file. In fact, the whole mapping file could be avoided if I include the right annotations in the ecore and use the suitable configuration options. This is something I have to try.

Reading about annotations, I have not found anyone to force the commit order, but I do not have enough knowledge about ecore annotations to claim that the order is just implied from the bare ecore and annotations cannot alter it.

Regards,
Conchi Sanz

Re: foreign key problems saving an EMF model [message #1284418 is a reply to message #1284348] Fri, 04 April 2014 12:58 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Conchi,
Ha sorry I missed that post, can you point to where in the ecore I should check?

I think cascade annotations can help somewhat in controlling commit ordering.

gr. Martin

On 04/04/2014 01:19 PM, Conchi Sanz wrote:
>
> Hi Martin, sorry for the ping-pong effect. The message sent on Thu, 03 April 2014 04:27 included a basic ecore model. If
> it is not enough I will send you a larger one, but will not contain new information, just more clases and more
> dependencies among them. Any other class has association relations apart from Root.
>
> You are right, if something changes in the ecore, the mapping will also have to change. In that case what I would do it
> is reordering the associations in the ecore itself and derived the mapping file. In fact, the whole mapping file could
> be avoided fif I include the right annotations in the ecore and use the suitable configuration options. This is something
> I have to try.
>
> Reading about annotations, I have not found anyone to force the commit order, but I do not have enough knowledge about
> ecore annotations to claim that the order is just implied from the bare ecore and annotations cannot alter it.
>
> Regards,
> Conchi Sanz
>
>


--

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: foreign key problems saving an EMF model [message #1286957 is a reply to message #1284418] Mon, 07 April 2014 08:27 Go to previous messageGo to next message
Conchi Sanz is currently offline Conchi SanzFriend
Messages: 10
Registered: December 2013
Junior Member
Hi Martin,
really sorry for the example, I sent the wrong one, too complex. Here I send you the right versión. The focus should be in DocumentRoot, where FOLDER should be commited before REL_FOLDER.

Right now there is no annotations in the ecore, since all are in the mapping file where all the associations are marked as
lazy="true" cascade="all,delete-orphan".


Regards,
Conchi Sanz
PD: I include the ecore in the message since I am not able to upload any file.


<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="flatSchema" nsURI="http:///flatSchema" nsPrefix="flatSchema">
<eAnnotations source="http://www.eclipse.org/OCL/Import">
<details key="ecore" value="http://www.eclipse.org/emf/2002/Ecore"/>
</eAnnotations>
<eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
<details key="invocationDelegates" value="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot"/>
<details key="settingDelegates" value="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot"/>
<details key="validationDelegates" value="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot"/>
</eAnnotations>
<eClassifiers xsi:type="ecore:EClass" name="DocumentRoot">
<eStructuralFeatures xsi:type="ecore:EReference" name="REL_FOLDER" ordered="false"
upperBound="-1" eType="#//REL_FOLDER" derived="true" containment="true" resolveProxies="false"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="FOLDER" ordered="false"
upperBound="-1" eType="#//FOLDER" derived="true" containment="true" resolveProxies="false"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="FOLDER">
<eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
<details key="constraints" value="primaryKey"/>
</eAnnotations>
<eAnnotations source="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot">
<details key="primaryKey" value="FOLDER.allInstances()-> isUnique(FOLDER_ID)"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_ID" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"
unsettable="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_NAME" lowerBound="1"
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="REL_FOLDER">
<eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
<details key="constraints" value="foreignKey1 foreignKey2 primaryKey"/>
</eAnnotations>
<eAnnotations source="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot">
<details key="foreignKey1" value="FOLDER.allInstances()-> exists(a|a.FOLDER_ID=FOLDER_PARENT)"/>
<details key="foreignKey2" value="FOLDER.allInstances()-> exists(a|a.FOLDER_ID=FOLDER_CHILD)"/>
<details key="primaryKey" value="REL_FOLDER.allInstances()-> isUnique(E_ID)"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_PARENT" lowerBound="1"
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_CHILD" lowerBound="1"
eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="E_ID" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"/>
</eClassifiers>
</ecore:EPackage>

[Updated on: Mon, 07 April 2014 11:34]

Report message to a moderator

Re: foreign key problems saving an EMF model [message #1288391 is a reply to message #1286957] Tue, 08 April 2014 14:19 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Conchi,
Yes I see, the REL_FOLDER has 2 eattributes, which model the relationship between 2 folder objects. If you model these
eattributes as ereferences you probably get Hibernate to correctly determine the persistence order.

gr. Martin

On 04/07/2014 10:27 AM, Conchi Sanz wrote:
> Hi Martin,
> really sorry for the example, I sent the wrong one, too complex. Here I send you the right versión. The focus should be
> in DocumentRoot, where FOLDER should be commited before REL_FOLDER.
> Right now there is no annotations in the ecore, since all are in the mapping file where all the associations are marked
> as lazy="true" cascade="all,delete-orphan".
>
>
> Regards,
> Conchi Sanz
> PD: I include the ecore in the message since I am not able to upload any file.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="flatSchema" nsURI="http:///flatSchema" nsPrefix="flatSchema">
> <eAnnotations source="http://www.eclipse.org/OCL/Import">
> <details key="ecore" value="http://www.eclipse.org/emf/2002/Ecore"/>
> </eAnnotations>
> <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
> <details key="invocationDelegates" value="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot"/>
> <details key="settingDelegates" value="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot"/>
> <details key="validationDelegates" value="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot"/>
> </eAnnotations>
> <eClassifiers xsi:type="ecore:EClass" name="DocumentRoot">
> <eStructuralFeatures xsi:type="ecore:EReference" name="REL_FOLDER" ordered="false"
> upperBound="-1" eType="#//REL_FOLDER" derived="true" containment="true" resolveProxies="false"/>
> <eStructuralFeatures xsi:type="ecore:EReference" name="FOLDER" ordered="false"
> upperBound="-1" eType="#//FOLDER" derived="true" containment="true" resolveProxies="false"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="FOLDER">
> <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
> <details key="constraints" value="primaryKey"/>
> </eAnnotations>
> <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot">
> <details key="primaryKey" value="GP_FOLDER_SCRIPT.allInstances()-> isUnique(GP_FOLDER_SCRIPT_ID)"/>
> </eAnnotations>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_ID" eType="ecore:EDataType
> http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"
> unsettable="true"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_NAME" lowerBound="1"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="REL_FOLDER">
> <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
> <details key="constraints" value="foreignKey1 foreignKey2 primaryKey"/>
> </eAnnotations>
> <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot">
> <details key="foreignKey1" value="GP_FOLDER_SCRIPT.allInstances()->
> exists(a|a.GP_FOLDER_SCRIPT_ID=GP_FOLDER_SCRIPT_PARENT)"/>
> <details key="foreignKey2" value="GP_FOLDER_SCRIPT.allInstances()->
> exists(a|a.GP_FOLDER_SCRIPT_ID=GP_FOLDER_SCRIPT_CHILD)"/>
> <details key="primaryKey" value="GP_REL_FOLDER_SCRIPT.allInstances()-> isUnique(E_ID)"/>
> </eAnnotations>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_PARENT" lowerBound="1"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="FOLDER_CHILD" lowerBound="1"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="E_ID" eType="ecore:EDataType
> http://www.eclipse.org/emf/2002/Ecore#//EBigInteger"/>
> </eClassifiers>
> </ecore:EPackage>
>


--

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: foreign key problems saving an EMF model [message #1289434 is a reply to message #1288391] Wed, 09 April 2014 10:18 Go to previous message
Conchi Sanz is currently offline Conchi SanzFriend
Messages: 10
Registered: December 2013
Junior Member

Hi Martin,

yes, replacing attributes with references makes sense regarding the persistence order. In fact, your suggestions has made me consider new options for the ecore. Smile

Once again, thanks for your time.

Regards,
Conchi Sanz
Previous Topic:[CDO] NullPointerException at org.eclipse.emf.cdo.internal.server.Repository.ensureChunk
Next Topic:EMF composite change
Goto Forum:
  


Current Time: Fri Apr 26 22:44:45 GMT 2024

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

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

Back to the top