Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Teneo/Hibernate/ModelRelational/Teneo Annotation Examples


This page lists examples of annotations in XML and directly in the model (XSD and ecore). Several complete examples can be downloaded.

The examples project

The model used in the examples project also contains annotations, both in the model itself (ecore) as well as in a separate XML file. After downloading the example projects from GIT go to the org.eclipse.emf.teneo.hibernate.examples project.


To see the annotations in the model open the extlibrary.ecore file in the model folder. The annotations are set here:

  • Book.category: @Enumerated to force the storage of an int in the database (instead of the Teneo default of using a String).
  • Library.writers: has a @IdBag annotation (Hibernate specific), an id bag is an efficient Hibernate list mapping, see here for more information.


The annotations xml file can be found in the src directory of the examples project. It is located in the org.eclipse.emf.teneo.hibernate.examples. The annotations xml specifies that the library object is stored in a table called 'TheLibrary'.

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

This zip file contains examples of annotations in xsd and ecore using EAnnotations.

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.

Back to the top