Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc) » JPA Annotation for Composite Foregin Key
| | |
Re: JPA Annotation for Composite Foregin Key [message #89116 is a reply to message #89101] |
Wed, 11 July 2007 21:11 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Yes, I see, multiple join columns on a many-to-one are not yet supported. I am currently working on
a new release. If you enter a bugzilla for this then I can try to incorporate this in this next
release (within a week).
gr. Martin
Vaibhav Kulkarni wrote:
> I get following exception :
>
> Caused by: org.eclipse.emf.teneo.hibernate.mapper.MappingException:
> Unsupported multiple join columns, EReference/subclass
> at
> org.eclipse.emf.teneo.hibernate.mapper.ManyToOneMapper.proce ss(ManyToOneMapper.java:64)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.FeatureMapper.process PAnnotatedEReference(FeatureMapper.java:161)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.FeatureMapper.process (FeatureMapper.java:97)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.EntityMapper.processF eature(EntityMapper.java:553)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.EntityMapper.processF eatures(EntityMapper.java:438)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.EntityMapper.processE ntity(EntityMapper.java:335)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.HibernateMappingGener ator.processPAClass(HibernateMappingGenerator.java:194)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.HibernateMappingGener ator.processPersistentClasses(HibernateMappingGenerator.java :162)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.HibernateMappingGener ator.generate(HibernateMappingGenerator.java:124)
>
> ... 7 more
>
> I am cut pasting annotations in ecore ..
>
> <eClassifiers xsi:type="ecore:EClass" name="RelationshipClass">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="relationname"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> <eStructuralFeatures xsi:type="ecore:EReference" name="subclass"
> eType="#//Subclass">
> <eAnnotations source="teneo.jpa">
> <details key="appinfo"
> value=" @JoinColumns({@JoinColumn(name="owning_id" ,
> referencedColumnName="id"),@JoinColumn(nam e="owning_ver",
> referencedColumnName="version"),@JoinColum n(name="owning_type",
> referencedColumnName="type")})"/>
> </eAnnotations>
> </eStructuralFeatures>
> </eClassifiers>
>
> RelationshipClass has 0 to 1 relationship to class named "Subclass".
> This Subclass has its primary key as id, type and version. So I want to
> add this 3 columns in RelationshipClass table with customized names (as
> shown above) as foreign keys.
>
>
> is this right annotation to be used to solve composite forign key issue ?
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
|
|
| |
Re: JPA Annotation for Composite Foregin Key [message #89146 is a reply to message #89131] |
Wed, 11 July 2007 21:57 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------080706000602050300090804
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
I have a testcase which covers this case as well as a one-to-many using a composite-key. I have
attached the ecore and the resulting hbm so you can see if there are any differences with your approach.
gr. martin
Vaibhav Kulkarni wrote:
> I have similar issue for composite primary keys when
> @Inheritance(strategy="JOINED") is used between concrete parent class
> and its concrete sub class. I want only composite parent keys are added
> in subclass table as a foreign keys to parent class. Parent table should
> have only properties in Parent class and Child table should have only
> properties in Child subclass.
>
> I get following exception :
>
> Caused by: org.hibernate.MappingException: Foreign key
> (FK6F2A564026128853:relationshipclass [subclass_subclass_id])) must have
> same number of columns as the referenced primary key (subclass
> [id,type,version])
> at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.jav a:90)
> at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.jav a:73)
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
--------------080706000602050300090804
Content-Type: text/xml;
name="JoinColumnsTest$1_mysql_s_o_hibernate.hbm.xml"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="JoinColumnsTest$1_mysql_s_o_hibernate.hbm.xml"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by Teneo on Wed Jul 11 23:53:48 CEST 2007 -->
<hibernate-mapping auto-import="false">
<class name=" org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.im pl.PersonImpl " entity-name="Person" abstract="false" lazy="false" table="`person`">
<meta attribute="eclassName">Person</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns</meta>
<composite-id class=" org.eclipse.emf.teneo.samples.emf.annotations.compositeid.Pe rsonID " mapped="true">
<key-property name="firstName" type="java.lang.String">
<column not-null="true" unique="false" name="`firstname`"/>
</key-property>
<key-property name="lastName" type="java.lang.String">
<column not-null="true" unique="false" name="`lastname`"/>
</key-property>
</composite-id>
<version name="myversion" column="myversion" access=" org.eclipse.emf.teneo.hibernate.mapping.property.VersionProp ertyHandler ">
<meta attribute="syntheticVersion">true</meta>
</version>
</class>
<joined-subclass name=" org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.im pl.ParentImpl " entity-name="Parent" abstract="false" lazy="false" extends="Person" table="`parent`">
<meta attribute="eclassName">Parent</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns</meta>
<key>
<column name="`person_firstname`"/>
<column name="`person_lastname`"/>
</key>
<list name="children" lazy="true" cascade="merge,persist,save-update,lock,refresh">
<key update="true">
<column name="`myparentfirstname`" not-null="false" unique="false"/>
<column name="`myparentlastname`" not-null="false" unique="false"/>
</key>
<list-index column="`parent_children_idx`"/>
<one-to-many entity-name="Child"/>
</list>
</joined-subclass>
<joined-subclass name=" org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.im pl.ChildImpl " entity-name="Child" abstract="false" lazy="false" extends="Person" table="`child`">
<meta attribute="eclassName">Child</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns</meta>
<key>
<column name="`person_firstname`"/>
<column name="`person_lastname`"/>
</key>
</joined-subclass>
</hibernate-mapping>
--------------080706000602050300090804
Content-Type: text/xml;
name="jc.ecore"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="jc.ecore"
<?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="joincolumns"
nsURI=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns" nsPrefix="joincolumns">
<eClassifiers xsi:type="ecore:EClass" name="Parent" eSuperTypes="#//Person">
<eStructuralFeatures xsi:type="ecore:EReference" name="children" upperBound="-1"
eType="#//Child">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value=" @JoinColumns({@JoinColumn(name="myParentFirstName&a mp;quot; referencedColumnName="firstName"),
 ;	@JoinColumn(name="myParentLastName&qu ot; referencedColumnName="lastName")})"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Child" eSuperTypes="#//Person"/>
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value=" @IdClass(org.eclipse.emf.teneo.samples.emf.annotations.compo siteid.PersonID) "/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="firstName" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Id"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="lastName" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Id"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
</ecore:EPackage>
--------------080706000602050300090804--
|
|
| |
Re: JPA Annotation for Composite Foregin Key [message #89605 is a reply to message #89591] |
Thu, 12 July 2007 21:27 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------000406030309050207020308
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
See my comments inline.
gr. Martin
Vaibhav Kulkarni wrote:
> I have noticed few differences as well as I am confused with the test
> case result and DDL it generates.
>
> 1) I am using @EmbeddedId and @Embeddeable and test case uses @IdClass.
> Hopefullly they should work the same way. It works for me for one class.
> But as soon as I extend that class it doesnt.
MT>> I attached also a testcase which has embeddedid. Unfortunately it is simpler
than the joincolumn one.
>
> 2) Test ecore shows 0 to many relation ship between Parent and Child.
> That means it should create join table 4 columns (parent_firstname,
> parent_lastname, child_firstname , child_lastname) .. Its doing some
> thing different. Its instead adding these columns into child table . If
> relation ships is shown between Parent and Child , then shouldnt it
> create tables accordingly ?
MT>> It is a relation without a join table, so then the foreign key is placed
in the child table.
With a join table it would look differently but I did not test that case.
>
> 3) If Parent has foreign key to Person , then why its adding Persons
> primary key to Child table. One can go from Child to Parent and then to
> Person to pick up all colums from Parent .
MT>> Also child inherits from Person, therefore there is a foreign key from the child table
to the person table.
>
> I will try to see if @IdClass works but I do see with representing these
> relationships in the data base.
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
--------------000406030309050207020308
Content-Type: text/xml;
name="embeddedid.ecore"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="embeddedid.ecore"
<?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="embeddedid"
nsURI=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid" nsPrefix="embeddedid">
<eClassifiers xsi:type="ecore:EClass" name="Name">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Embeddable"/>
</eAnnotations>
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Name"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="firstName" unique="false"
lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//String">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="firstName"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="lastName" unique="false"
lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//String">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="lastName"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Person"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="name" lowerBound="1" eType="#//Name"
containment="true" resolveProxies="false">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@EmbeddedId"/>
</eAnnotations>
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="name"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="address" unique="false"
eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//String">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Lob"/>
</eAnnotations>
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="address"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
</ecore:EPackage>
--------------000406030309050207020308
Content-Type: text/xml;
name="EmbeddedId.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="EmbeddedId.xsd"
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:this=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid">
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:element name="name" type="this:Name">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@EmbeddedId</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="address" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Lob</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Name">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Embeddable</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
--------------000406030309050207020308
Content-Type: text/xml;
name="EmbeddedIdAction_mysql_s_o_hibernate.hbm.xml"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="EmbeddedIdAction_mysql_s_o_hibernate.hbm.xml"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by Teneo on Thu Jul 12 19:26:02 CEST 2007 -->
<hibernate-mapping auto-import="false">
<class name=" org.eclipse.emf.teneo.samples.emf.annotations.embeddedid.imp l.PersonImpl " entity-name="Person" abstract="false" lazy="false" table="`person`">
<meta attribute="eclassName">Person</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid</meta>
<composite-id name="name" class=" org.eclipse.emf.teneo.samples.emf.annotations.embeddedid.imp l.NameImpl ">
<key-property name="firstName" type="java.lang.String">
<column not-null="true" unique="false" name="`firstname`"/>
</key-property>
<key-property name="lastName" type="java.lang.String">
<column not-null="true" unique="false" name="`lastname`"/>
</key-property>
</composite-id>
<version name="e_version" column="e_version" access=" org.eclipse.emf.teneo.hibernate.mapping.property.VersionProp ertyHandler ">
<meta attribute="syntheticVersion">true</meta>
</version>
<property name="address" lazy="false" insert="true" update="true" not-null="false" unique="false" type="text">
<column not-null="false" unique="false" name="`address`"/>
</property>
</class>
</hibernate-mapping>
--------------000406030309050207020308--
|
|
| |
Re: JPA Annotation for Composite Foregin Key [message #90168 is a reply to message #89967] |
Tue, 17 July 2007 15:25 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Vaibhav,
See my comments in line.
Vaibhav Kulkarni wrote:
> I have summarized issues after doing some more evaluation around JPA
> annotations.
>
> 1) Join table is not created for many to many or many to one non
> contained associations even though Teneo persistence option of
> "JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS=true" WHEN primary key is
> composed of more than one columns. When I changed Primary Key to contain
> only one column , it worked well & I did not had to specify @JoinTable
> annotations. As I beleive this option is created to avoid specifying
> explicit @JoinTable annotation. Pls fix this and test it using composite
> primary key expresedd using @IdClass or @Embedeable/@EmbeddedId
MT>> Yes I am looking into this.
Btw, to me (as a non-native english speaker) the phrase 'Pls fix this and test it' sounds kind of
commandeering, probably you don't mean it that way...
>
> 2) If JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS option is set to "true"
> and @JoinColumns annotations is used then it does create DDL and adds
> foreign key columns in the table for class representing other side of
> the relation ship . I think this is covered in your Person/Child/Parent
> test case where columns are added in Child table even though "children"
> relationship is present in Parent class. This works with both single
> column PK as well as composite PK. But does than mean @JoinColumns
> annotations take precedence over
> JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS ?
MT>> No the option should take precedence, if it doesn't then it is a bug. As I am looking into this
topic (of composite-ids) right now I will also check it.
> 3) Same thing true for INHERITANCE_MAPPING=JOINED settings. It works
> for single column PK but not for composite PK. Once composite PK is
> defined it fails to add one column in the table (representing subclass)
> for every column in PK of parent class. Once fixed it also needs to be
> tested with JPA annotation of @Inheritance(strategy="JOINED") to check
> if it gives same result as INHERITANCE_MAPPING=JOINED settings.
MT> The person-child testcase I sent you earlier covers this case, all test cases are run for single
and joined inheritance mapping.
>
> 4) Came across below exception when attempt was made to customize
> inverse column names using @JoinTable annotation.
MT>> I will adapt the testcase to also include a jointable with a composite-id, hopefully this
exception will also appear there.
>
> Caused by: org.xml.sax.SAXParseException: Attribute "update" must be
> declared for element type "many-to-many".
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper. createSAXParseException(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper. error(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.rep ortError(Unknown
> Source)
>
> Heres is annotation :
>
> "appinfo", "@JoinTable(name=\"parent_child\",
> joinColumns=@JoinColumn(name=\"parent_FirstName\"
> referencedColumnName=\"firstName\"), " +
>
> "inverseJoinColumns=@JoinColumn(name=\"child_FirstName\"
> referencedColumnName=\"firstName\") )"
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
|
|
| | | | | | | | | | | | | |
Re: JPA Annotation for Composite Foregin Key [message #608705 is a reply to message #89101] |
Wed, 11 July 2007 21:11 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Yes, I see, multiple join columns on a many-to-one are not yet supported. I am currently working on
a new release. If you enter a bugzilla for this then I can try to incorporate this in this next
release (within a week).
gr. Martin
Vaibhav Kulkarni wrote:
> I get following exception :
>
> Caused by: org.eclipse.emf.teneo.hibernate.mapper.MappingException:
> Unsupported multiple join columns, EReference/subclass
> at
> org.eclipse.emf.teneo.hibernate.mapper.ManyToOneMapper.proce ss(ManyToOneMapper.java:64)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.FeatureMapper.process PAnnotatedEReference(FeatureMapper.java:161)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.FeatureMapper.process (FeatureMapper.java:97)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.EntityMapper.processF eature(EntityMapper.java:553)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.EntityMapper.processF eatures(EntityMapper.java:438)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.EntityMapper.processE ntity(EntityMapper.java:335)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.HibernateMappingGener ator.processPAClass(HibernateMappingGenerator.java:194)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.HibernateMappingGener ator.processPersistentClasses(HibernateMappingGenerator.java :162)
>
> at
> org.eclipse.emf.teneo.hibernate.mapper.HibernateMappingGener ator.generate(HibernateMappingGenerator.java:124)
>
> ... 7 more
>
> I am cut pasting annotations in ecore ..
>
> <eClassifiers xsi:type="ecore:EClass" name="RelationshipClass">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="relationname"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> <eStructuralFeatures xsi:type="ecore:EReference" name="subclass"
> eType="#//Subclass">
> <eAnnotations source="teneo.jpa">
> <details key="appinfo"
> value=" @JoinColumns({@JoinColumn(name="owning_id" ,
> referencedColumnName="id"),@JoinColumn(nam e="owning_ver",
> referencedColumnName="version"),@JoinColum n(name="owning_type",
> referencedColumnName="type")})"/>
> </eAnnotations>
> </eStructuralFeatures>
> </eClassifiers>
>
> RelationshipClass has 0 to 1 relationship to class named "Subclass".
> This Subclass has its primary key as id, type and version. So I want to
> add this 3 columns in RelationshipClass table with customized names (as
> shown above) as foreign keys.
>
>
> is this right annotation to be used to solve composite forign key issue ?
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
|
|
| |
Re: JPA Annotation for Composite Foregin Key [message #608707 is a reply to message #89131] |
Wed, 11 July 2007 21:57 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------080706000602050300090804
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
I have a testcase which covers this case as well as a one-to-many using a composite-key. I have
attached the ecore and the resulting hbm so you can see if there are any differences with your approach.
gr. martin
Vaibhav Kulkarni wrote:
> I have similar issue for composite primary keys when
> @Inheritance(strategy="JOINED") is used between concrete parent class
> and its concrete sub class. I want only composite parent keys are added
> in subclass table as a foreign keys to parent class. Parent table should
> have only properties in Parent class and Child table should have only
> properties in Child subclass.
>
> I get following exception :
>
> Caused by: org.hibernate.MappingException: Foreign key
> (FK6F2A564026128853:relationshipclass [subclass_subclass_id])) must have
> same number of columns as the referenced primary key (subclass
> [id,type,version])
> at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.jav a:90)
> at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.jav a:73)
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
--------------080706000602050300090804
Content-Type: text/xml;
name="JoinColumnsTest$1_mysql_s_o_hibernate.hbm.xml"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="JoinColumnsTest$1_mysql_s_o_hibernate.hbm.xml"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by Teneo on Wed Jul 11 23:53:48 CEST 2007 -->
<hibernate-mapping auto-import="false">
<class name=" org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.im pl.PersonImpl " entity-name="Person" abstract="false" lazy="false" table="`person`">
<meta attribute="eclassName">Person</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns</meta>
<composite-id class=" org.eclipse.emf.teneo.samples.emf.annotations.compositeid.Pe rsonID " mapped="true">
<key-property name="firstName" type="java.lang.String">
<column not-null="true" unique="false" name="`firstname`"/>
</key-property>
<key-property name="lastName" type="java.lang.String">
<column not-null="true" unique="false" name="`lastname`"/>
</key-property>
</composite-id>
<version name="myversion" column="myversion" access=" org.eclipse.emf.teneo.hibernate.mapping.property.VersionProp ertyHandler ">
<meta attribute="syntheticVersion">true</meta>
</version>
</class>
<joined-subclass name=" org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.im pl.ParentImpl " entity-name="Parent" abstract="false" lazy="false" extends="Person" table="`parent`">
<meta attribute="eclassName">Parent</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns</meta>
<key>
<column name="`person_firstname`"/>
<column name="`person_lastname`"/>
</key>
<list name="children" lazy="true" cascade="merge,persist,save-update,lock,refresh">
<key update="true">
<column name="`myparentfirstname`" not-null="false" unique="false"/>
<column name="`myparentlastname`" not-null="false" unique="false"/>
</key>
<list-index column="`parent_children_idx`"/>
<one-to-many entity-name="Child"/>
</list>
</joined-subclass>
<joined-subclass name=" org.eclipse.emf.teneo.samples.emf.annotations.joincolumns.im pl.ChildImpl " entity-name="Child" abstract="false" lazy="false" extends="Person" table="`child`">
<meta attribute="eclassName">Child</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns</meta>
<key>
<column name="`person_firstname`"/>
<column name="`person_lastname`"/>
</key>
</joined-subclass>
</hibernate-mapping>
--------------080706000602050300090804
Content-Type: text/xml;
name="jc.ecore"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="jc.ecore"
<?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="joincolumns"
nsURI=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/joi ncolumns" nsPrefix="joincolumns">
<eClassifiers xsi:type="ecore:EClass" name="Parent" eSuperTypes="#//Person">
<eStructuralFeatures xsi:type="ecore:EReference" name="children" upperBound="-1"
eType="#//Child">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value=" @JoinColumns({@JoinColumn(name="myParentFirstName&a mp;quot; referencedColumnName="firstName"),
 ;	@JoinColumn(name="myParentLastName&qu ot; referencedColumnName="lastName")})"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Child" eSuperTypes="#//Person"/>
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value=" @IdClass(org.eclipse.emf.teneo.samples.emf.annotations.compo siteid.PersonID) "/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="firstName" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Id"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="lastName" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Id"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
</ecore:EPackage>
--------------080706000602050300090804--
|
|
| |
Re: JPA Annotation for Composite Foregin Key [message #608737 is a reply to message #89591] |
Thu, 12 July 2007 21:27 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------000406030309050207020308
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
See my comments inline.
gr. Martin
Vaibhav Kulkarni wrote:
> I have noticed few differences as well as I am confused with the test
> case result and DDL it generates.
>
> 1) I am using @EmbeddedId and @Embeddeable and test case uses @IdClass.
> Hopefullly they should work the same way. It works for me for one class.
> But as soon as I extend that class it doesnt.
MT>> I attached also a testcase which has embeddedid. Unfortunately it is simpler
than the joincolumn one.
>
> 2) Test ecore shows 0 to many relation ship between Parent and Child.
> That means it should create join table 4 columns (parent_firstname,
> parent_lastname, child_firstname , child_lastname) .. Its doing some
> thing different. Its instead adding these columns into child table . If
> relation ships is shown between Parent and Child , then shouldnt it
> create tables accordingly ?
MT>> It is a relation without a join table, so then the foreign key is placed
in the child table.
With a join table it would look differently but I did not test that case.
>
> 3) If Parent has foreign key to Person , then why its adding Persons
> primary key to Child table. One can go from Child to Parent and then to
> Person to pick up all colums from Parent .
MT>> Also child inherits from Person, therefore there is a foreign key from the child table
to the person table.
>
> I will try to see if @IdClass works but I do see with representing these
> relationships in the data base.
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
--------------000406030309050207020308
Content-Type: text/xml;
name="embeddedid.ecore"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="embeddedid.ecore"
<?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="embeddedid"
nsURI=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid" nsPrefix="embeddedid">
<eClassifiers xsi:type="ecore:EClass" name="Name">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Embeddable"/>
</eAnnotations>
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Name"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="firstName" unique="false"
lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//String">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="firstName"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="lastName" unique="false"
lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//String">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="lastName"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Person">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="Person"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="name" lowerBound="1" eType="#//Name"
containment="true" resolveProxies="false">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@EmbeddedId"/>
</eAnnotations>
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="name"/>
</eAnnotations>
</eStructuralFeatures>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="address" unique="false"
eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//String">
<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Lob"/>
</eAnnotations>
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="kind" value="element"/>
<details key="name" value="address"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
</ecore:EPackage>
--------------000406030309050207020308
Content-Type: text/xml;
name="EmbeddedId.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="EmbeddedId.xsd"
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:this=" http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid">
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:element name="name" type="this:Name">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@EmbeddedId</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="address" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Lob</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Name">
<xsd:annotation>
<xsd:appinfo source="teneo.jpa">@Embeddable</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
--------------000406030309050207020308
Content-Type: text/xml;
name="EmbeddedIdAction_mysql_s_o_hibernate.hbm.xml"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="EmbeddedIdAction_mysql_s_o_hibernate.hbm.xml"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by Teneo on Thu Jul 12 19:26:02 CEST 2007 -->
<hibernate-mapping auto-import="false">
<class name=" org.eclipse.emf.teneo.samples.emf.annotations.embeddedid.imp l.PersonImpl " entity-name="Person" abstract="false" lazy="false" table="`person`">
<meta attribute="eclassName">Person</meta>
<meta attribute="epackage"> http://www.eclipse.org/emf/teneo/samples/emf/annotations/emb eddedid</meta>
<composite-id name="name" class=" org.eclipse.emf.teneo.samples.emf.annotations.embeddedid.imp l.NameImpl ">
<key-property name="firstName" type="java.lang.String">
<column not-null="true" unique="false" name="`firstname`"/>
</key-property>
<key-property name="lastName" type="java.lang.String">
<column not-null="true" unique="false" name="`lastname`"/>
</key-property>
</composite-id>
<version name="e_version" column="e_version" access=" org.eclipse.emf.teneo.hibernate.mapping.property.VersionProp ertyHandler ">
<meta attribute="syntheticVersion">true</meta>
</version>
<property name="address" lazy="false" insert="true" update="true" not-null="false" unique="false" type="text">
<column not-null="false" unique="false" name="`address`"/>
</property>
</class>
</hibernate-mapping>
--------------000406030309050207020308--
|
|
| |
Re: JPA Annotation for Composite Foregin Key [message #608772 is a reply to message #89967] |
Tue, 17 July 2007 15:25 |
Martin Taal Messages: 5468 Registered: July 2009 |
Senior Member |
|
|
Hi Vaibhav,
See my comments in line.
Vaibhav Kulkarni wrote:
> I have summarized issues after doing some more evaluation around JPA
> annotations.
>
> 1) Join table is not created for many to many or many to one non
> contained associations even though Teneo persistence option of
> "JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS=true" WHEN primary key is
> composed of more than one columns. When I changed Primary Key to contain
> only one column , it worked well & I did not had to specify @JoinTable
> annotations. As I beleive this option is created to avoid specifying
> explicit @JoinTable annotation. Pls fix this and test it using composite
> primary key expresedd using @IdClass or @Embedeable/@EmbeddedId
MT>> Yes I am looking into this.
Btw, to me (as a non-native english speaker) the phrase 'Pls fix this and test it' sounds kind of
commandeering, probably you don't mean it that way...
>
> 2) If JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS option is set to "true"
> and @JoinColumns annotations is used then it does create DDL and adds
> foreign key columns in the table for class representing other side of
> the relation ship . I think this is covered in your Person/Child/Parent
> test case where columns are added in Child table even though "children"
> relationship is present in Parent class. This works with both single
> column PK as well as composite PK. But does than mean @JoinColumns
> annotations take precedence over
> JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS ?
MT>> No the option should take precedence, if it doesn't then it is a bug. As I am looking into this
topic (of composite-ids) right now I will also check it.
> 3) Same thing true for INHERITANCE_MAPPING=JOINED settings. It works
> for single column PK but not for composite PK. Once composite PK is
> defined it fails to add one column in the table (representing subclass)
> for every column in PK of parent class. Once fixed it also needs to be
> tested with JPA annotation of @Inheritance(strategy="JOINED") to check
> if it gives same result as INHERITANCE_MAPPING=JOINED settings.
MT> The person-child testcase I sent you earlier covers this case, all test cases are run for single
and joined inheritance mapping.
>
> 4) Came across below exception when attempt was made to customize
> inverse column names using @JoinTable annotation.
MT>> I will adapt the testcase to also include a jointable with a composite-id, hopefully this
exception will also appear there.
>
> Caused by: org.xml.sax.SAXParseException: Attribute "update" must be
> declared for element type "many-to-many".
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper. createSAXParseException(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper. error(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.rep ortError(Unknown
> Source)
>
> Heres is annotation :
>
> "appinfo", "@JoinTable(name=\"parent_child\",
> joinColumns=@JoinColumn(name=\"parent_FirstName\"
> referencedColumnName=\"firstName\"), " +
>
> "inverseJoinColumns=@JoinColumn(name=\"child_FirstName\"
> referencedColumnName=\"firstName\") )"
>
>
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
|
|
| | | | | | | | | | | |
Goto Forum:
Current Time: Mon Sep 23 08:47:35 GMT 2024
Powered by FUDForum. Page generated in 0.32020 seconds
|