Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo] Mapping a combined primary key with teneo
[Teneo] Mapping a combined primary key with teneo [message #1757860] Mon, 20 March 2017 19:43 Go to next message
Felix S is currently offline Felix SFriend
Messages: 10
Registered: November 2014
Junior Member
Hi,

I have a generated DB-Scheme, which I want to operate on with ecore and Teneo/Hibernate. To do so, I created a similar ecore model. This worked good so far. But now I encounter a problem with a table (DataValue in this case), which has a combined primary key of three columns (see DataValue_PK) and this table being referenced by another table (ValueConstraintValue) with this combined primary key (see ValueCsnrValue_FK2).

The part of interest of the scheme looks like this
CREATE TABLE DataType (
	DataTypeID NVARCHAR2(128) NOT NULL,
	CONSTRAINT DataT_PK PRIMARY KEY(DataTypeID),
	-- skip some irrelevant stuff
);

CREATE TABLE DataValue (
	DataTypeID NVARCHAR2(128) NOT NULL,
	Value NVARCHAR2(512) NOT NULL,
	LanguageCode NVARCHAR2(85) NOT NULL,
	CONSTRAINT DataValue_PK PRIMARY KEY(DataTypeID, Value, LanguageCode),
	CONSTRAINT DataValue_FK FOREIGN KEY (DataTypeID)  REFERENCES DataType (DataTypeID)
);

CREATE TABLE ValueConstraintValue (
	DataTypeID NVARCHAR2(128) NOT NULL,
	Value NVARCHAR2(512) NOT NULL,
	LanguageCode NVARCHAR2(85) NOT NULL,
	ValueQID NVARCHAR2(128) NOT NULL,
	CONSTRAINT ValueCsnrValue_PK PRIMARY KEY(DataTypeID, Value, LanguageCode, ValueQID),
	CONSTRAINT ValueCsnrValue_FK1 FOREIGN KEY (ValueQID) REFERENCES ValueConstraint (ValueQID),
	CONSTRAINT ValueCsnrValue_FK2 FOREIGN KEY (DataTypeID, Value, LanguageCode) REFERENCES DataValue (DataTypeID, Value, LanguageCode)
);

CREATE TABLE ValueConstraint (
	ValueQID NVARCHAR2(128) NOT NULL,
	CONSTRAINT ValueCsnr_PK PRIMARY KEY(ValueQID),
	-- skip some more irrelevant stuff
);


Is there any chance, to reverse map this situation in ecore? I tried to annotate the DataValue-EClass with the Hibernate-Annotation @PrimaryKeyJoinColumns, but this didn't work.

As mentioned in the beginning, the scheme is generated by a tool, so I have few chances of altering the scheme, since it would affect other things in the scheme. So I hope, there is a solution for this problem.

Best regards
Re: [Teneo] Mapping a combined primary key with teneo [message #1757867 is a reply to message #1757860] Mon, 20 March 2017 23:07 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Felix,
Yes this is possible. You have to create a separate java class (does not need to be an eobject) and use it to encapsulate the primary key columns.

This ecore model here:
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?h=2.1.0

shows an example of a composite id with 2 eattributes. The PersonID class encapsulates the 2 columns. Note the implementation of the equals and hash methods, this is important to do correctly. Here is the test class:
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

Here is another example if the primary key fields include a foreign key:
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 its related testclass:
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

gr. Martin
Re: [Teneo] Mapping a combined primary key with teneo [message #1757892 is a reply to message #1757867] Tue, 21 March 2017 12:43 Go to previous messageGo to next message
Felix S is currently offline Felix SFriend
Messages: 10
Registered: November 2014
Junior Member
Hi Martin,

thanks for the quick response Smile

I implemented that ID-Class as suggested and annotated the DataValue-EClass as following

@IdClass(teneo.test.DataValueID)
@PrimaryKeyJoinColumns({
	@PrimaryKeyJoinColumn(name="DataTypeID"),
	@PrimaryKeyJoinColumn(name="Value"),
	@PrimaryKeyJoinColumn(name="LanguageCode")
})


Regenerating and running this adjusted code gives me the following Exception:

java.lang.ExceptionInInitializerError
  ...
  Caused by: org.hibernate.MappingException: Foreign key (valueconstraintvalue_datavalue:valueconstraintvalue [datatypeid,value,languagecode])) must have same number of columns as the referenced primary key (datavalue [e_id])


In the log I see also a message, saying that the PrimaryKeyJoinColumn-Annotation is ignored, since DataValue is a root in the class hierarchy.

Mär 21, 2017 11:47:17 AM org.eclipse.emf.teneo.hibernate.mapper.EntityMapper processEntity
WARNUNG: This entity (DataValue is the root in the class hierarchy and has a pk joincolum annotation, this is not correct, ignoring pk joincolumn annotation


I thought, that PrimaryKeyJoinColumns-Annotation is allowed for root class in an one-to-one-association, isn't it? Did I missed something?

Best regards
Re: [Teneo] Mapping a combined primary key with teneo [message #1759710 is a reply to message #1757892] Mon, 17 April 2017 07:26 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Felix,
Later response (I don't get notifications from the forum it seems on replies). The primarykeyjoincolumn is needed to join a subclass to the root class table. But in this case I don't see a root-class-table in your database schema. So the DataValue should be the root of the class-hierarchy and would not need a primary key join column. Only an @IdClass annotation.

gr. Martn

Re: [Teneo] Mapping a combined primary key with teneo [message #1760304 is a reply to message #1759710] Tue, 25 April 2017 16:04 Go to previous message
Felix S is currently offline Felix SFriend
Messages: 10
Registered: November 2014
Junior Member
yes, you're right. Without PrimaryKeyJoinColumn it works fine. Thanks
Previous Topic:the diagram components messy when opened in editor
Next Topic:[CDO] Wiki Performances tips
Goto Forum:
  


Current Time: Thu Mar 28 09:47:12 GMT 2024

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

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

Back to the top