Appendix B. Annotations Reference

Table of Contents

B.1. javax.persistence
B.1.1. @javax.persistence.Basic
B.1.2. @javax.persistence.Column
B.1.3. @javax.persistence.DiscriminatorValue
B.1.4. @javax.persistence.Embeddable
B.1.5. @javax.persistence.Entity
B.1.6. @javax.persistence.FetchType
B.1.7. @javax.persistence.Id
B.1.8. @javax.persistence.JoinColumn
B.1.9. @javax.persistence.ManyToOne
B.1.10. @javax.persistence.NamedQueries and @javax.persistence.NamedQuery
B.1.11. @javax.persistence.OneToOne
B.1.12. @javax.persistence.Transient
B.1.13. @javax.persistence.Version
B.2. org.hibernate.annotations
B.2.1. @org.hibernate.annotations.AnyMetaDef
B.2.2. @org.hibernate.annotations.CollectionOfElements
B.2.3. @org.hibernate.annotations.Entity
B.2.4. @org.hibernate.annotations.Immutable

This appendix describes the annotations that are identified by Naked Objects and whose semantics are captured in the Naked Objects metamodel. The presence of some of these are reflected in the Naked Objects viewers, others are required by JPA Objects itself.

Note that these are not the only annotations that can be applied; in general most JPA/Hibernate annotations will work. However, there are a number of annotations that are not supported, namely those that define composite primary keys. See Section 2.3, “Specify a primary key using @Id (not using @IdClass or @EmbeddedId)” for further discussion.

B.1. javax.persistence

B.1.1. @javax.persistence.Basic

Applies only to properties. Used by JPA to describe a simple value property.

For Naked Objects, it determines:

  • whether the property is mandatory or not, using @Basic(optional=...)

  • the fetch type; @Basic(fetch=...). This isn't used by Naked Objects viewer but is stored in the metamodel.

B.1.2. @javax.persistence.Column

Applies only to properties. Used by JPA to specify details about the column in the database table.

For Naked Objects, it determines:

  • whether the property is mandatory or not, using @Column(nullable=...)

  • for a string property, its maximum length, using @Column(length=...)

B.1.3. @javax.persistence.DiscriminatorValue

Applies only to classes. Used by JPA to distinguish subclass entities (see Section B.1.5, “@javax.persistence.Entity) within inheritance hierarchies.

Whereas in "regular" JPA it is only necessary to annotate entities with inheritance, JPA Objects makes this a mandatory requirement for all entities that are not embeddable (see Section B.1.4, “@javax.persistence.Embeddable). This is for two reasons:

B.1.4. @javax.persistence.Embeddable

Applies to classes. Used by JPA to indicate that a class is not standalone, in other words is embeddable within another entity (see Section B.1.5, “@javax.persistence.Entity) by way of an property annotated with @Embedded (see Section 4.7, “Embedded Objects”).

In domain-driven design terms an embeddable object is an aggregated object, and this semantic is captured in the Naked Objects metamodel. In addition, JPA Objects ensures that every domain class is annotated as either an @Entity or @Embeddable.

B.1.5. @javax.persistence.Entity

Applies to classes, indicating that the class represents a persistent entity.

For Naked Objects, @javax.persistence.Entity is captured in the metamodel and is used for validation; every domain class must be annotated as either an @Entity or @Embeddable (see Section B.1.4, “@javax.persistence.Embeddable). See Chapter 2, JPA Objects / Naked Objects Restrictions for more details. It isn't otherwise used.

See also the Hibernate specific annotation, Section B.2.3, “@org.hibernate.annotations.Entity.

B.1.6. @javax.persistence.FetchType

Applies to properties and collections. Used by JPA/Hibernate, to specify lazy loading characteristics.

Not used by Naked Objects, but captured in the metamodel (primarily as a side-effect of capturing semantics by other annotations that do provide other relevant info, eg @org.hibernate.annotations.CollectionOfElements, Section B.2.2, “@org.hibernate.annotations.CollectionOfElements).

B.1.7. @javax.persistence.Id

Applies only to properties. Used by JPA to identify the property to use as the primary key of an entity (see Section B.1.5, “@javax.persistence.Entity).

In JPA every entity must be identified, and while this is typically done using @Id, it is also possible to use composite keys. However, JPA Objects does not support composite keys; every entity must be annotated with @Id.

The reasoning is the same as that for making discriminator values (see Section B.1.3, “@javax.persistence.DiscriminatorValue) mandatory:

For Naked Objects, the @Id annotation implies that the property is both mandatory and disabled.

B.1.8. @javax.persistence.JoinColumn

Applies only to properties. Used by JPA to specify a mapped column for joining an entity association, in other words the name of the foreign key table for a @ManyToOne (Section B.1.9, “@javax.persistence.ManyToOne) or a @OneToOne (Section B.1.11, “@javax.persistence.OneToOne) association.

For Naked Objects, determines:

  • whether the property is optional, using @JoinColumn(nullable=...)

B.1.9. @javax.persistence.ManyToOne

Applies only to properties. Used by JPA to represent a many-to-one (child/parent) relationship, indicating the property on a "child" entity that is a reference to the "parent" entity.

For Naked Objects, determines:

  • whether the property is optional, using @ManyToOne(optional=...)

  • fetch type, from @ManyToOne(fetch=...); not used by Naked Objects but captured in the meta-model

See also Section B.1.11, “@javax.persistence.OneToOne.

B.1.10. @javax.persistence.NamedQueries and @javax.persistence.NamedQuery

Both of these annotations apply only to classes, and in particular to entities (see Section B.1.5, “@javax.persistence.Entity). They define a named query/set of named queries) which return the annotated entity, for use in repository implementations. See Chapter 5, Implementing Repositories for further details.

B.1.11. @javax.persistence.OneToOne

Applies only to properties. Used by JPA to represent a one-to-one relationship, indicating the property on a "referencing" entity to a "referenced" entity.

For Naked Objects, determines:

  • whether the property is optional, using @OneToOne(optional=...)

  • fetch type, from @OneToOne(fetch=...); not used by Naked Objects but captured in the meta-model

See also Section B.1.9, “@javax.persistence.ManyToOne.

B.1.12. @javax.persistence.Transient

Applies only to properties. Used by JPA to indicate that the property is not persistent.

For Naked Objects, determines:

  • that the property is derived (cf a property with only a getter and no setter)

B.1.13. @javax.persistence.Version

Applies only to properties. Used by JPA to specify the propert that serves as its optimistic lock value.

For Naked Objects, the @Version annotation implies that the property is both mandatory and disabled.

For further discussion on optimistic locking, see Section 8.1, “Optimistic Locking”.