Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [TEXO] Adding annotations
[TEXO] Adding annotations [message #819268] Mon, 12 March 2012 17:44 Go to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi,

I want to add a new attribute called id and annotate it with @Id and @GeneratedValue to each object generated by TEXO from an XSD. I believe I need to edit the ecore file. I have tried adding various annotations to the ecore model but so far none have work. Can you help me work out how to accomplish this please.

A simplified version of the XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="/PurchaseOrder" xmlns:xs="/2001/XMLSchema">
	<xs:element name="title" type="xs:string"/>
	<xs:element name="firstname" type="xs:string"/>
	<xs:element name="surname" type="xs:string"/>
	<xs:element name="age" type="xs:int"/>
	<xs:element name="name" type="xs:string"/>
	<xs:element name="street" type="xs:string"/>
	<xs:element name="city" type="xs:string"/>
	<xs:element name="state" type="xs:string"/>
	<xs:element name="zip" type="xs:integer"/>
	<xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
	<xs:attribute name="OrderDate" type="xs:date"/>
</xs:schema>


Regards,
Gary
Re: [TEXO] Adding annotations [message #819294 is a reply to message #819268] Mon, 12 March 2012 18:29 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5332
Registered: July 2009
Senior Member
Hi Gary,
There are different approaches to this, depending if you control the model or not.

>> Don't control the model (so the model can't be changed)
If your model is in an ecore file (so not xsd) then you can use an annotation model to annotation the efeatures/elements
which model the id and composite id:
http://wiki.eclipse.org/Texo/ORM_JPA_Annotations_Details
You can create an ecore model from an xsd by right clicking it and then in the Texo submenu generate the ecore file,
then create the annotations model, then when generating code right click on the ecore file (not the xsd) to generate
code using the annotations model.

Another approach is to handcode a class which has the id member and annotate it with @MappedSuperclass, then in the
annotations model set the Root Class Extends property on an epackage annotation, let its value be the qualified
classname of the mapped super class. This is somewhat easier as you don't need to annotate each individual id efeature.

>> Control the model, can change it
If you control the model, instead of handcoding this superclass (see previous point) you can also make it part of your
model and let the other classes inherit from it. But then you need to control the model.

gr. Martin

On 03/12/2012 06:44 PM, Gary Godfrey wrote:
> Hi,
>
> I want to add a new attribute called id and annotate it with @Id and @GeneratedValue to each object generated by TEXO
> from an XSD. I believe I need to edit the ecore file. I have tried adding various annotations to the ecore model but so
> far none have work. Can you help me work out how to accomplish this please.
>
> A simplified version of the XSD:
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema targetNamespace="/PurchaseOrder" xmlns:xs="/2001/XMLSchema">
> <xs:element name="title" type="xs:string"/>
> <xs:element name="firstname" type="xs:string"/>
> <xs:element name="surname" type="xs:string"/>
> <xs:element name="age" type="xs:int"/>
> <xs:element name="name" type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="city" type="xs:string"/>
> <xs:element name="state" type="xs:string"/>
> <xs:element name="zip" type="xs:integer"/>
> <xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
> <xs:attribute name="OrderDate" type="xs:date"/>
> </xs:schema>
>
>
> Regards,
> Gary


--

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@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [TEXO] Adding annotations [message #820031 is a reply to message #819294] Tue, 13 March 2012 16:35 Go to previous messageGo to next message
Gary Godfrey is currently offline Gary Godfrey
Messages: 31
Registered: February 2012
Member
Hi Martin,

I want to persist the ECore model generated from an XSD so want to add an ID attribute to each entity. I have written a simple JPAIdentifer class:

package com.foobar.jpa;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public class JPAIdentifier
{
	@Id
	@GeneratedValue
	private long id;
	
	public void setId(long id)
	{this.id = id;}

	public long getId() 
	{return id;}
}


I have created the ecore file and from this I have created Code Gen Annotations Model. To this I have supplied the Root Class Extends value.

When I generate the code, the superclass attributes are not being added. Can you help me determine what I am doing wrong please?

Regards,
Gary
  • Attachment: texo_app.png
    (Size: 31.88KB, Downloaded 92 times)
  • Attachment: simple.xsd
    (Size: 6.46KB, Downloaded 78 times)
Re: [TEXO] Adding annotations [message #820055 is a reply to message #820031] Tue, 13 March 2012 17:09 Go to previous messageGo to next message
Martin Taal is currently offline Martin Taal
Messages: 5332
Registered: July 2009
Senior Member
Hi Gary,
Do you generate the code when right clicking on the ecore file or on the xsd? It should be through the ecore file. Also
I remember that your xsd results in 2 ecore files, you can check if you are indeed annotating the correct epackage.

As a tip, it is better to start with an almost empty annotations model, in your case you only need the annotation on
epackage level, the rest can be deleted.This because any changes in the ecore model are not synced to the annotations
model, so when you do changes in the ecore model the annotations model can get out of sync.

gr. Martin

On 03/13/2012 05:35 PM, Gary Godfrey wrote:
> Hi Martin,
>
> I want to persist the ECore model generated from an XSD so want to add an ID attribute to each entity. I have written a simple JPAIdentifer class:
>
>
> package com.foobar.jpa;
>
> import javax.persistence.GeneratedValue;
> import javax.persistence.Id;
> import javax.persistence.MappedSuperclass;
>
> @MappedSuperclass
> public class JPAIdentifier
> {
> @Id
> @GeneratedValue
> private long id;
>
> public void setId(long id)
> {this.id = id;}
>
> public long getId()
> {return id;}
> }
>
>
> I have created the ecore file and from this I have created Code Gen Annotations Model. To this I have supplied the Root Class Extends value.
>
> When I generate the code, the superclass attributes are not being added. Can you help me determine what I am doing wrong please?
>
> Regards,
> Gary


--

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@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [TEXO] Adding annotations [message #820106 is a reply to message #820055] Tue, 13 March 2012 18:32 Go to previous message
Martin Taal is currently offline Martin Taal
Messages: 5332
Registered: July 2009
Senior Member
And as an extra tip.. the annotationsmodel file should be called the same as the ecore model file except for the
extension... I will for sure document this better.

gr. Martin

On 03/13/2012 06:09 PM, Martin Taal wrote:
> Hi Gary,
> Do you generate the code when right clicking on the ecore file or on the xsd? It should be through the ecore file. Also
> I remember that your xsd results in 2 ecore files, you can check if you are indeed annotating the correct epackage.
>
> As a tip, it is better to start with an almost empty annotations model, in your case you only need the annotation on
> epackage level, the rest can be deleted.This because any changes in the ecore model are not synced to the annotations
> model, so when you do changes in the ecore model the annotations model can get out of sync.
>
> gr. Martin
>
> On 03/13/2012 05:35 PM, Gary Godfrey wrote:
>> Hi Martin,
>>
>> I want to persist the ECore model generated from an XSD so want to add an ID attribute to each entity. I have written
>> a simple JPAIdentifer class:
>>
>>
>> package com.foobar.jpa;
>>
>> import javax.persistence.GeneratedValue;
>> import javax.persistence.Id;
>> import javax.persistence.MappedSuperclass;
>>
>> @MappedSuperclass
>> public class JPAIdentifier
>> {
>> @Id
>> @GeneratedValue
>> private long id;
>>
>> public void setId(long id)
>> {this.id = id;}
>>
>> public long getId()
>> {return id;}
>> }
>>
>>
>> I have created the ecore file and from this I have created Code Gen Annotations Model. To this I have supplied the
>> Root Class Extends value.
>>
>> When I generate the code, the superclass attributes are not being added. Can you help me determine what I am doing
>> wrong please?
>>
>> Regards,
>> Gary
>
>


--

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@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Previous Topic:[TEXO] ecore generation > 1 top level EPackage
Next Topic:[emffacet] Drop a model element onto a FacetReferenceColumn
Goto Forum:
  


Current Time: Fri Sep 19 12:09:01 GMT 2014

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

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