Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "Teneo/Hibernate/ModelRelational/Teneo Annotation Examples"

(Examples of annotations in XML)
(The examples project)
 
(9 intermediate revisions by one other user not shown)
Line 4: Line 4:
 
This page lists examples of annotations in XML and directly in the model (XSD and ecore). Several complete examples can be downloaded.
 
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==
+
==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 [[Teneo/Hibernate/Download_and_Install#Download_and_Install_Example_Projects_for_Tutorials|downloading]] the example projects from GIT go to the org.eclipse.emf.teneo.hibernate.examples project.
  
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==
+
To see the annotations in the model open the extlibrary.ecore file in the model folder. The annotations are set here:
Here are a number of examples of annotations defined in the XML schema model file:
+
* Book.category: @Enumerated to force the storage of an int in the database (instead of the Teneo default of using a String).
  
* Basic annotations: xsd, ecore (Generated HBM)
+
* Library.writers: has a @IdBag annotation (Hibernate specific), an id bag is an efficient Hibernate list mapping, see [http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-idbag here] for more information.
  
* 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)
+
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'.
  
* Enumerated and fetch with subselect example: xsd, ecore (Generated HBM)
+
==Examples of annotations in XML==
 +
 
 +
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==
  
 +
This [[Media:Teneo_Model_EAnnotations_Examples.zip|zip file]] contains examples of annotations in xsd and ecore using EAnnotations.
  
 
==Lob Annotation==
 
==Lob Annotation==
Line 56: Line 60:
 
</source>
 
</source>
 
 
 
 
==Annotations on EDataType==
 
==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 :
 
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 :
Line 129: Line 132:
 
</source>
 
</source>
 
 
 
 
==ManyToMany Annotations==
 
==ManyToMany Annotations==
 
An example of the use of a ManyToMany annotation:
 
An example of the use of a ManyToMany annotation:
Line 179: Line 181:
 
</xsd:complexType>
 
</xsd:complexType>
 
</source>
 
</source>
 
 
 
 
==Inheritance and Discriminator Annotations==
 
==Inheritance and Discriminator Annotations==
Line 211: Line 212:
 
</source>
 
</source>
 
 
For other examples of annotations on this site see here: Inheritance and Associations.
+
For other examples of annotations on this site see here: [[Teneo/Hibernate/ModelRelational/Inheritance_Mapping|Inheritance]] and [[Teneo/Hibernate/ModelRelational/Association_Mapping|Associations]].
 
+
 
+
==Associations==
+
 
+
----
+
Wikis: [[CDO]] | [[Net4j]] | [[EMF]] | [[Eclipse]]
+

Latest revision as of 03:53, 30 March 2012


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