I am learning JPA from this tutorial.
I have some confusions in understanding the following annotations:
@Basic
@Embedded
In ORM mapping, the granularity of your object model can be finer than that of your database.
For example, you can have a Person
record in your database which can be further decomposed to contain a reference to an Address
object in your model. That's where the @Embedded
and @Embeddable
annotations come in. They simply state a relationship where one Entity
can be stored as part of another.
As for the @Basic
annotation, it's the simplest form of mapping which is applied by default to primitive types such as int and float and their wrappers as well as enums. More information can be had here: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-property
The @Embeddable
annotation allows to specify a class whose instances are stored as intrinsic part of the owning entity. This annotation has no attributes.
@Embeddable
public class EmploymentPeriod {
java.util.Date startDate;
java.util.Date endDate;
...
}
The @Embedded
annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class. By default, column definitions specified in the @Embeddable
class apply to the table of the owning entity but you can override them using@AttributeOverride
:
@Embedded
@AttributeOverrides({
@AttributeOverride(name="startDate", column=@Column(name="EMP_START")),
@AttributeOverride(name="endDate", column=@Column(name="EMP_END"))
})
public EmploymentPeriod getEmploymentPeriod() { ... }
Regarding the optional @Basic
annotation, you may use it to configure the fetch type to LAZY
and to configure the mapping to forbid null values (for non primitive types) with the optional
attribute.
@Basic(fetch=LAZY)
protected String getName() { return name; }
You can also place it on a field or property to explicitly mark it as persistent (for documentation purpose).
@Basic
The Basic annotation can be applied to a persistent property or instance variable of any of the following types:
Java primitive types, wrappers of the primitive types, String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements java.io.Serializable.
The use of the Basic annotation is optional for persistent fields and properties of these types. If the Basic annotation is not specified for such a field or property, the default values of the Basic annotation will apply.
Example:
@Basic
protected String name;
and
@Basic(fetch=LAZY)
protected String getName() {
return name;
}
@Embedded
Specifies a persistent field or property of an entity whose value is an instance of an embeddable class. The embeddable class must be annotated as Embeddable.
Example 1:
@Embedded
@AttributeOverrides({
@AttributeOverride(name="startDate", column=@Column("EMP_START")),
@AttributeOverride(name="endDate", column=@Column("EMP_END"))
})
public EmploymentPeriod getEmploymentPeriod() { ... }
Example 2:
@Entity
public class Project {
@EmbeddedId ProjectId id;
//other fields
}
@Embeddable
Class ProjectId {
int departmentId;
long projectId;
}
JSR Persistence Specification and Source reference