Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Teneo/Hibernate/ModelRelational/Teneo Annotation Examples"
(→Examples of annotations in XML) |
(→Examples of annotations in XML) |
||
Line 6: | Line 6: | ||
==Examples of annotations in XML== | ==Examples of annotations in XML== | ||
− | This [[ | + | This [[Media:Teneo_XML_Annotations_Examples.zip|zip file]] contains several examples of annotations specified in a separate xml with the accompanying ecore model. |
==Examples of annotations defined in Ecore or in a XML Schema model== | ==Examples of annotations defined in Ecore or in a XML Schema model== |
Revision as of 08:00, 21 January 2010
Contents
This page lists examples of annotations in XML and directly in the model (XSD and ecore). Several complete examples can be downloaded.
Examples of annotations in XML
This zip file contains several examples of annotations specified in a separate xml with the accompanying ecore model.
Examples of annotations defined in Ecore or in a XML Schema model
Here are a number of examples of annotations defined in the XML schema model file:
- Basic annotations: xsd, ecore (Generated HBM)
- More complex example with many-to-one, column and composite-id related annotations: xsd, ecore (Generated HBM)
- Embedded and Associationoverride annotation example: xsd, ecore (Generated HBM)
- Enumerated and fetch with subselect example: xsd, ecore (Generated HBM)
Lob Annotation
Here is an example on how to handle blob's using ecore and model annotations.
First add an EDataType for java.sql.Blob in your ecore:
<eClassifiers xsi:type="ecore:EDataType" name="Blob" instanceClassName="java.sql.Blob"/>
Then, in the attribute of type Blob, add "teneo.jpa" annotations as follows in your ecore:
.... <eStructuralFeatures xsi:type="ecore:EAttribute" name="content" eType="#//Blob"> <eAnnotations source="teneo.jpa"> <details key="value" value=" @Lob @Column(length=1048576) @Type(type="blob")"/> </eAnnotations> </eStructuralFeatures> ...
Notice the 1MB column length if you copy-paste. Change it to what you need.
Note also that the above annotation can be set on the EDataType. In that case it won't be needed to specify it on each EAttribute.
Then, use the blob as you would with hibernate. For instance:
Document doc = MyFactory.eINSTANCE.createDocument(); Blob blob = Hibernate.createBlob(new byte[] { 1, 2, 3, 4 }); doc.setContent(blob);
Annotations on EDataType
An example of the use of a Table annotation at EClass level and Column annotations on EDataType level, first in xml and then using the :
<epackage namespace-uri="http://www.eclipse.org/emf/teneo/samples/emf/annotations/edatatype_column"> <eclass name="Book"> <table name="mybooktable"/> <property name="title"> <column name="titel" unique="true" length="25"/> </property> </eclass> <edatatype name="TitleType"> <column name="mytitle" unique="false" length="50"/> </edatatype> <edatatype name="PagesType"> <column updatable="false" insertable="false"/> </edatatype> <edatatype name="WeightType"> <column name="gewicht" nullable="true" precision="5" scale="2"/> </edatatype> </epackage>
And in java annotation syntax in EAnnotations:
<xsd:complexType name="Book"> <xsd:annotation> <xsd:appinfo source="teneo.jpa">@Table(name="mybooktable")</xsd:appinfo> </xsd:annotation> <xsd:sequence> <xsd:element name="title" type="TitleType"> <xsd:annotation> <xsd:appinfo source="teneo.jpa">@Column(name="titel" unique="true" length="25")</xsd:appinfo> </xsd:annotation> </xsd:element> <xsd:element name="pages" type="PagesType"/> <xsd:element name="weight" type="WeightType"/> <xsd:element name="author" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="TitleType"> <xsd:annotation> <xsd:appinfo source="teneo.jpa">@Column(name="mytitle" unique="false" length="50")</xsd:appinfo> </xsd:annotation> <xsd:restriction base="xsd:string"> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="PagesType"> <xsd:annotation> <xsd:appinfo source="teneo.jpa">@Column(updatable="false" insertable="false")</xsd:appinfo> </xsd:annotation> <xsd:restriction base="xsd:int"> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="WeightType"> <xsd:annotation> <xsd:appinfo source="teneo.jpa">@Column(name="gewicht" nullable="true" precision="5" scale="2")</xsd:appinfo> </xsd:annotation> <xsd:restriction base="xsd:decimal"> </xsd:restriction> </xsd:simpleType>
ManyToMany Annotations
An example of the use of a ManyToMany annotation:
<xsd:complexType name="Cntr"> <xsd:sequence> <xsd:element name="rght" type="xsd:IDREF" ecore:reference="this:Rght" maxOccurs="unbounded" ecore:opposite="cntr"> <xsd:annotation> <xsd:appinfo source="teneo.jpa"> @ManyToMany(fetch=EAGER cascade={MERGE, PERSIST} targetEntity="Rght" indexed="false") @JoinTable(name="RightCenter") </xsd:appinfo> </xsd:annotation> </xsd:element> <xsd:element name="lft" type="xsd:IDREF" ecore:reference="this:Lft" maxOccurs="unbounded" ecore:opposite="cntr"> <xsd:annotation> <xsd:appinfo source="teneo.jpa"> @ManyToMany(fetch=EAGER cascade={MERGE, PERSIST} targetEntity="Lft") </xsd:appinfo> </xsd:annotation> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Lft"> <xsd:sequence> <xsd:element name="cntr" type="xsd:IDREF" ecore:reference="this:Cntr" maxOccurs="unbounded" ecore:opposite="lft"> <xsd:annotation> <xsd:appinfo source="teneo.jpa"> @ManyToMany(fetch=LAZY cascade={MERGE,PERSIST} targetEntity="Cntr" mappedBy="lft") </xsd:appinfo> </xsd:annotation> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Rght"> <xsd:sequence> <xsd:element name="cntr" type="xsd:IDREF" ecore:reference="this:Cntr" maxOccurs="unbounded" ecore:opposite="rght"> <xsd:annotation> <xsd:appinfo source="teneo.jpa"> @ManyToMany(fetch=LAZY cascade={MERGE, PERSIST} targetEntity="Cntr" mappedBy="rght" indexed="false") @JoinTable(name="RightCenter") </xsd:appinfo> </xsd:annotation> </xsd:element> </xsd:sequence> </xsd:complexType>
Inheritance and Discriminator Annotations
An example of the use of an Inheritance and Discriminator related annotations:
<xsd:complexType name="Price"> <xsd:annotation> <xsd:appinfo source="teneo.jpa"> @Table(name="myprice") @Inheritance(strategy=SINGLE_TABLE) @DiscriminatorColumn(name="DISCRIMINATOR" discriminatorType=STRING) @DiscriminatorValue("myPrice") </xsd:appinfo> </xsd:annotation> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="value" type="xsd:decimal"/> </xsd:sequence> </xsd:complexType>
In xml:
<eclass name="Price"> <table name="myprice" /> <inheritance>SINGLE_TABLE</inheritance> <discriminator-column name="DISCRIMINATOR" discriminator-type="STRING" /> <discriminator-value>myPrice</discriminator-value> </eclass>
For other examples of annotations on this site see here: Inheritance and Associations.