Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Teneo] Problem setting a primary composite key that is also a foreign key
[Teneo] Problem setting a primary composite key that is also a foreign key [message #1720132] Thu, 14 January 2016 15:45 Go to next message
Maxi Schick is currently offline Maxi SchickFriend
Messages: 2
Registered: January 2016
Junior Member
Hi,

i am currently trying to reverse engineer an existing database structure into an Ecore model with Teneo. I created an Ecore model that closely resembles the model of the existing database structure with Xcore. Now i am using Teneo to configure the relational mapping.

One eclass has a composite primary key consisting of a primitive String value and a foreign key. I am having trouble configuring Teneo to resemble that behavior.

The Xcore file
class Attribute {
	contains AttributeConstraint[] Constraints opposite Attribute
}

class AttributeConstraint {
	container Attribute Attribute opposite TaggedValues

	String	Value
}

I must configure Teneo via an annotations.xml file, because the Ecore model is still being altered.

The annotations.xml file

<eclass name="AttributeConstraint">
	<id-class>de.cooperateproject.eabridge.eaobjectmodel.custom.AttributeConstraintPK</id-class>
	<table name="t_attributeconstraints"/>
	<property name="Attribute">
       		<id/> 
          	<many-to-one></many-to-one>
       		<join-column name="ID"/>
       	</property> 
         <property name="Constraint">
        	<id/>
  	</property>


The id-class file
public class AttributeConstraintPK implements Serializable {

	private static final long serialVersionUID = 6403908284592273608L;
	
	protected String Constraint;
	
        protected Long Attribute;

	public AttributeConstraintPK(String constraint, Long attribute) {
		super();
		Constraint = constraint;
		Attribute = attribute;
	}

	public AttributeConstraintPK() {
		super();
		// TODO Auto-generated constructor stub
	}

	// hashcode, equals, getter & setter
	
}


With this setup the <id/> annotation for the foreign key keeps getting ignored. I had success manually writing the hibernate.hbm.xml file, but that is not a good solution imho.

Also is there a reference sheet for the xml scheme of Teneo? I tried configuring it like the hibernate xml schema and had no success. After some hours of trying i understood that it is somewhat similiar to the hibernate Java annotation syntax. Is that assumption true?
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1720181 is a reply to message #1720132] Thu, 14 January 2016 23:20 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Maxi,
Yes composite ids are always tricky. Here is an example of a composite id test model persisted using teneo:
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/tests/org.eclipse.emf.teneo.samples/src/org/eclipse/emf/teneo/samples/emf/annotations/compositeid

Here is the test case:
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/emf/annotations/CompositeIdAction.java

Here is the location of the xsd (I updated the wiki, the link was broken there)
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/hibernate/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/annotations/persistence-mapping.xsd

Btw, depending on what your plans are you can also consider the EMFT Texo project, this project uses JPA inline in the code generated from an annotation model:
https://wiki.eclipse.org/Texo

gr. Martin
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1720838 is a reply to message #1720181] Thu, 21 January 2016 11:00 Go to previous messageGo to next message
Maxi Schick is currently offline Maxi SchickFriend
Messages: 2
Registered: January 2016
Junior Member
Hello Martin,
thanks for your quick response. Unfortunately, the example you provided did not help me finding a correct annotation setup.
In the example the composite primary key of the Person object consists of two primitive Attributes. In my problem however the composite primary key consists of one primitive attribute and a foreign key.
I can not get teneo to use the foreign key column as part of the primary key.

gr. Maxi
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1721810 is a reply to message #1720838] Sun, 31 January 2016 09:15 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Maxi,
I made an example with a foreign key composite id. I had to make some changes to make it work, these are in the latest build available on the update site. Here is the example model (with annotations in the model's ecore):
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/tests/org.eclipse.emf.teneo.samples/src/org/eclipse/emf/teneo/samples/emf/annotations/compositefkid?h=2.1.0

and here is the testcase:
http://git.eclipse.org/c/teneo/org.eclipse.emf.teneo.git/tree/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/emf/annotations/CompositeFKIdAction.java?h=2.1.0

Let me know if you need more info.

gr. Martin
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1733449 is a reply to message #1720132] Fri, 27 May 2016 15:05 Go to previous messageGo to next message
Stephan Seifermann is currently offline Stephan SeifermannFriend
Messages: 19
Registered: March 2015
Junior Member
Hello Martin,

your example is very helpful. I applied it in one of my scenarios but I ran into an issue that results in an endless loop. Maybe you got an idea how to solve it.

I modeled two classes in Xcore:
class ClassA {
	Long ID
	contains ClassB[] theChildren opposite theParent
}

class ClassB {
	container ClassA theParent opposite theChildren
	String aName
}


The persistence mapping looks like this
<eclass name="ClassA">
	<property name="ID">
		<id />
	</property>
</eclass>

<eclass name="ClassB">
	<id-class>ClassBIDClass</id-class>
	<property name="theParent">
		<id />
		<join-column name="ParentID" />
	</property>
	<property name="aName">
		<column name="Name"/>
		<id />
	</property>
</eclass>


I created the foreign key class according to your example. This results in an endless loop because of the inverse adding at the parent and the child.

I tried to remove the opposite relations and created a derived reference in ClassB that serves as input for the key calculation. Unfortunately, the attribute is completely ignored for the ID generation in this case.

Do you have any idea how to fix my approach or do you have any other idea for solving this issue?

Best regards
Stephan
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1734003 is a reply to message #1733449] Thu, 02 June 2016 21:46 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Stephan,
And if you only do this then it works? (I know you want the opposite eref also, but just to see if it works):

class ClassA {
Long ID
}

class ClassB {
container ClassA theParent
String aName
}

and the same persistence mapping as you used below.

gr. Martin
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1734477 is a reply to message #1734003] Wed, 08 June 2016 15:41 Go to previous messageGo to next message
Stephan Seifermann is currently offline Stephan SeifermannFriend
Messages: 19
Registered: March 2015
Junior Member
Hello Martin,

thanks for your reply. I was on vacation, so sorry for the late response. The change suggested by you is not possible because you have to specify containment references (contains). Container statements have to have an opposite that is a containment relation.

I could remove the opposite tags, model a single containment in ClassA and the relation to the parent as regular reference or even a derived one in ClassB. That would be perfectly fine for me. Unfortunately, I do not know how to create a mapping for this.

I attached a minimal example that allows reproducing the issue. I would be very grateful if you could assist me in performing the changes you suggested.

Best regards
Stephan
  • Attachment: minimal.zip
    (Size: 58.22KB, Downloaded 120 times)
Re: [Teneo] Problem setting a primary composite key that is also a foreign key [message #1735520 is a reply to message #1720132] Mon, 20 June 2016 14:27 Go to previous message
Stephan Seifermann is currently offline Stephan SeifermannFriend
Messages: 19
Registered: March 2015
Junior Member
We did some experiments in the meantime but we only got the mapping working by manually maintaining the attribute reference and using the Hibernate mapping XML generated for the previous case that includes the opposite reference. This seems to work without the endless loop. Anyway, this solution is not good:

  • The model can become inconsistent because there is no relation between the containment reference and the opposite reference anymore.
  • We had to switch from the generated hibernate mapping XML to a manually maintained one to keep the mapping working (cf. the code snippets below).
Wrong:
<bag name="TheChildren" lazy="true" cascade="all,delete-orphan">
	<key foreign-key="ClassA_TheChildren_key" update="true">
		<column name="`ClassA_TheChildren_ID`" unique="false"/>
	</key>
	<one-to-many entity-name="ClassB"/>
</bag>
Correct:
<bag name="TheChildren" lazy="true" cascade="all,delete-orphan">
	<key update="true">
		<column name="`ParentID`" unique="false"/>
	</key>
	<one-to-many entity-name="ClassB"/>
</bag>


I would be grateful for every hint on how to handle these shortcomings.
Previous Topic:[EMFStore] How to resolve checksum errors?
Next Topic:using part of EMF model in Preferences
Goto Forum:
  


Current Time: Sat Apr 20 03:14:27 GMT 2024

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

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

Back to the top