HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Format of JPA/EJB3 Annotations as EAnnotations and as XML

Teneo allows two ways to specify EJB3/JPA-like annotations: 1) using the java annotations syntax in EAnnotations (examples), 2) a separate xml document (examples).

This page will show examples of both approaches.

A JPA Annotation can have different targets: TYPE, METHOD and FIELD (see the EJB3/JPA spec). Annotations with a TYPE target can be used for EMF EClasses, annotations with METHOD and FIELD targets can be used for EStructuralFeatures. Note: JPA annotations relevant for a non-reference type java member can also be set on an EDataType (see here for more information).

Note: when specifying enumerated values then the constant value of the enumeration should be used without the annotation class name. For example the annotation: @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) should (in Teneo) be written as: @Cache(usage = NONSTRICT_READ_WRITE) (without quotes)

JPA Annotations in EAnnotations using the java annotation syntax

The EAnnotations should adhere to the following format:

  • The source must be: teneo.jpa
  • The key must be: appinfo or value

See here for examples of how to define eannotations in Ecore or in a XML Schema file.

JPA Annotations in XML

JPA annotations can also be specified in a separate xml file. The advantage of this approach is that you can annotate models which are not in your control. The separate annotated xml means that the original model file does not need to be changed. You can even annotate the Ecore model itself! The xsd for the XML annotations can be downloaded here.

A number of examples of annotations in xml can be found here.

The xsd shows that it is possible to specify annotations on EPackage, EClass, EAttribute, EReference and EDataType level. In addition there is a special property element which combines the annotations for EAttribute and EReference. The property tag is a convenience tag which can be used to in place of both an EAttribute and an EReference tag.

When initializing a HbDataStore the location of the xml annotation file should be passed using the persistenceoption: PersistenceOptions.PERSISTENCE_XML. The location should be a resource path.

Simple Annotation Example

An example of an JPA annotation without a key-value pair is the Embedded annotation.

In a XML Schema model the Embedded annotation is specified as follows:

<xsd:element name="secondEmbedded" type="this:Embeddable">
	<xsd:annotation>
		<xsd:appinfo source="teneo.jpa">@Embedded</xsd:appinfo>
	</xsd:annotation>
</xsd:element>
			

An example of a (different) simple annotation in xml:

			
<eclass name="Name">
	<embeddable />
</eclass>
			

Annotation with key-value

An example of an EJB3 annotation with a key-value pair is the Basic annotation.

In a XML Schema model the Basic annotation is specified as follows:

<xsd:element name="myOptionalBasic" type="xsd:string">
	<xsd:annotation>
		<xsd:appinfo source="teneo.jpa">@Basic(optional=true fetch=EAGER)</xsd:appinfo>
	</xsd:annotation>
</xsd:element>
			

An example of a (different) key-value annotation in xml:

			
<property name="head">
	<one-to-one fetch="EAGER" target-entity="Head" optional="true" cascade="ALL" />
</property>
			

As you can see this example uses the property tag. The property tag is a convenience tag which can be used to in place of both an EAttribute and an EReference tag.

Complex Annotation with multi-level structure

An example of a more complex annotation is the SecondaryTable annotation which can contain a pkJoinColumns annotation:

<xsd:annotation>
	<xsd:appinfo source="teneo.jpa">
		@SecondaryTable(name="THETONER" pkJoinColumns={@PrimaryKeyJoinColumn(name="PRINTER_ID")})
	</xsd:appinfo>
</xsd:annotation>
			

An example of a (different) more complex annotation in xml:

			
<eclass name="Employee">
	<association-override name="address">
		<join-column name="employee_address_id"/>
	</association-override>
</eclass>