Hibernate and Elver Extension Annotations
About
In addition to the EJB3 annotations, Elver supports a number of custom annotations which are partially based on the Hibernate extensions: The hibernate specific annotation have the source hibernate.elver.org, the Elver specific annotations have annotation.elver.org as a source.
IdBag Annotation
The IdBag annotation causes collections to be mapped to a Hibernate <idbag> element. The main advantage of an <idbag> is that it usually performs much faster than a regular <bag>.
Note that <idbags> can only be used for
- value collections (for example a list of Strings)
- many-to-many entity relationships
- many-to-any entity relationships
One-to-many relationships are not supported by <idbag>.
Attributes
The IdBag annotation has the following attributes:
-
IdBag.type (optional, default: "long")
The type used for the ID column. -
IdBag.generator (optional, default: "increment")
The generator used for the ID column. Note that in the current Hibernate implementation "native" cannot be used as an <idbag> generator.
Example
The following XML Schema element:
<xsd:element name="role" type="xsd:string" minOccurs="0" maxOccurs="unbounded"> <xsd:annotation> <xsd:appinfo source="http://annotation.elver.org/IdBag">true</xsd:appinfo> </xsd:annotation> </xsd:element>
results in this Hibernate mapping:
<idbag name="role" lazy="true" cascade="all,delete-orphan"> <collection-id column="ID" type="long"> <generator class="increment"/> </collection-id> <key update="true"> <column name="`ROLE_ID`" not-null="false" unique="false"/> </key> <element type="java.lang.String"/> </idbag>
Type and Columns annotations
The Type and Columns annotations are used in conjunction to map custom value types to multiple columns.
Attributes
-
Type.type (required)
The fully-qualified class name of the UserType. -
Columns.value (required)
One or more Column annotations.
Example
In the example below the custom value type Name is mapped to the columns "first_name" and "last_name".
<xsd:simpleType name="Name" ecore:instanceClass="org.elver.samples.emf.hibernate.usertype.Name"> <xsd:annotation> <xsd:appinfo source="http://hibernate.elver.org/Type" ecore:key="type">org.elver.samples.emf.hibernate.usertype.NameType</xsd:appinfo> <xsd:appinfo source="http://hibernate.elver.org/Columns" ecore:key="value">c1 c2</xsd:appinfo> <xsd:appinfo source="http://ejb.elver.org/Column/c1" ecore:key="name">first_name</xsd:appinfo> <xsd:appinfo source="http://ejb.elver.org/Column/c2" ecore:key="name">last_name</xsd:appinfo> </xsd:annotation> <xsd:restriction/> </xsd:simpleType> <xsd:complexType name="Person"> <xsd:sequence> <xsd:element name="name" type="this:Name"> </xsd:element> </xsd:sequence> </xsd:complexType>
Indexed Annotation
The http://annotation.elver.org/Indexed annotation can be used to signal to Elver that a certain association does not require an index column and has a bag like behavior. See here for more information.
Unique Annotation
The http://annotation.elver.org/Unique annotation can be used to signal to Elver that a certain one-to-many association is not unique. This corresponds to a unidirectional one-to-many. This means that a join table should be used. See here for more information.