Embeddable Objects

In some cases you may want to embed plain Java objects or value objects inside your entities. These are called embedded value objects. Each persistent property of these embeddable value objects are mapped to column within the entity's table.

The @Embeddable annotation is typically used on the class whose instance is an embeddable value object of an entity. Only a limited number of annotations are permitted on the properties of the embeddable value object. The EJB3 cartridge defines the embeddable value object using an ORM metadata mapping file.

A persistent property of an entity which is an instance of an embeddable value object is marked with the @Embedded annotation. The @AttributeOverride and @AttributeOverrides annotations can override column mappings declared in the embedded value object.

  • Auto-generated source that does not need manual editing
  • Auto-generated source that should be edited manually
  • File that is affected by the modifications applied in this section
  • Person.java
  • PersonDao.java
  • PersonDaoBase.java
  • PersonDaoImpl.java
  • PersonDaoException.java
  • PersonName.java
  • PersonNameImpl.java
  • orm.xml

You can model basic features on your embeddable class attributes and these will be reflected in the generated embeddable value object. These features are much the same as described in the howto for entities. The following is a brief list of annotations and some properties that are available on embeddable attributes.

  • @Transient
  • @Version
  • @Lob with @Basic settings to fetch type
  • @Column with properties unique, nullable, insertable, updatable, length, columnDefinition, precision and scale
  • @Temporal
  • @Enumerated

Helpful Hints

The EJB3 cartridge now uses the persistence ORM mapping file orm.xml to declare the embeddable class metadata. This means that the @Embeddable annotation is no longer rendered in the base or implementation for the embeddable object. The ORM mapping file defines the embeddable implementation, which extends the abstract embeddable base object, as the embeddable class.

This has been adopted so that you can have an implementation for embeddable value object operations or any other aspect requiring manual implementation, without having to be concerned with code regeneration over-writing your implementation. This could not be achieved with pure annotations.

The cartridge now support having multiple attributes having the same embeddable value object type within a single entity. There is limited support for associations between embeddable value objects.


For JBoss Seam integration with your EJB 3.0 project, click here to continue.