41

I am learning JPA from this tutorial.

I have some confusions in understanding the following annotations:

  • @Basic
  • @Embedded

Fields of an embeddable type default to persistent, as if annotated with @Embedded.

If the fields of embeddable types default to persistent, then why would we need the @Embedded annotation

3 Answers 3

59

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Q1. If the fields or properties of embeddable types are by defualt persistent then why we need to add @Embedded annotation ? Q2. Can I use @AttributeOverrides annotation without @Embedded annotation? Could you please also throw some light on @Basic?
Q1. Because @Embedded allows to override column definitions Q2. No I don't think so. There is a good example illustrating this here: redhat.com/docs/en-US/JBoss_Enterprise_Web_Platform/5.0.0/html/…
@Pascal What if there are no overrides? What's the purpose of @Embedded? Can it be left out?
@HendyIrawan: I have posted this as a question here stackoverflow.com/q/31157700/2886891
Most usefull, concise and complete answer on this subject ! ;) got my +1
13

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

Comments

3

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.