Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

Hibernate : Difference between @ Embedded annotation technique and @OneToOne annotation Technique

What is the difference between @Embedded annotation technique and @OneToOne annotation technique because in Embedded the java class contain "Has a" relationship in class and with the help of @Embedded annotation we persist the has a object in database. and in OneToOne relationship we also persist the has a object in database.

Upvotes: 19

Views: 21432

Answers (5)

Shailesh Pratapwar
Shailesh Pratapwar

Reputation: 4224

Use @OneToOne, only if fields can be reused. Otherwise, go for @Embeddable.

A quote from Beginning Hibernte, 3rd Edition:

There is nothing intrinsically wrong with mapping a one-to-one association between two entities where one is not a component of (i.e., embedded into) the other. The relationship is often somewhat suspect, however. You should give some thought to using the embedded technique described previously before using the @OneToOne annotation.

@Embeddable: If the fields in an entity (X) are contained within the same table as another entity (Y), then entity X is called "component" in hibernate terms or "embedded" in JPA terms. In any case, JPA or hibernate do not allow to use 2nd table to store such embedded entities.

Generally, we think of normalizing a table when data is being reused by more than one table. Example: A Customer (id, name, street, city, pin, landmark) can be normalized into Customer(id, name) and CustomerAddress(cust_id, street, city, pin, landmark). In this case, we can reuse CustomerAddress by linking the same using cust_id with other tables. But if this reuse is not required in your application, then we can just keep all columns in one table.

So, a thumb rule is,

  • If reuse -> @OneToOne,
  • If no reuse -> @Embeddable

Upvotes: 3

Inder
Inder

Reputation: 170

@Embedded is used with Value Objects (Objects which have a meaning only when attached to an Object) whereas one to one mapping is between two objects having their own existence and meaning.

For e.g.

Value Object and @Embedded: If we have a User class and this class has an address Object in it, it can be considered as a value object as the address alone does not have any significance until unless associated with a user. Here address object can be annotated with @Embedded.

One to One mapping and @OneToOne: If we have a User class and this class has a 'Father' Object or a 'Mother' object, we would want to annotate the 'Father' or 'Mother' instance as @OneToOne as 'Father' or 'Mother' have their own meaning and existence and are not Value objects to User class.

A closely related difference is between @OneToMany and @ElementCollection. Both are used to save instance variables of Collection type in Java class. The difference being, @ElementCollection is to be used when the elements of Collection being saved are Value Objects whereas @OneToMany is used when the elments and object have well defined meaning and existence.

Upvotes: 5

Dimitri Dewaele
Dimitri Dewaele

Reputation: 10659

@Embedded is typically to represent a composite primary key as an embeddable class:

@Entity
public class Project {
    @EmbeddedId ProjectId id;
     :
}

@Embeddable
Class ProjectId {
    int departmentId;
    long projectId;
}

The primary key fields are defined in an embeddable class. The entity contains a single primary key field that is annotated with @EmbeddedId and contains an instance of that embeddable class. When using this form a separate ID class is not defined because the embeddable class itself can represent complete primary key values.

@OneToOne is for mapping two DB tables that are related with a one to one relationship. @Id will be the primary key.

Upvotes: 1

Ben Thurley
Ben Thurley

Reputation: 7141

@OneToOne is for mapping two DB tables that are related with a one to one relationship. For example a Customer might always have one record in a Name table.

Alternatively if those name fields are on the Customer table (not in a separate table) then you might want an @embedded. On the face of it you could just add the name fields as standard attributes to the Customer entity but it can be useful if those same columns appear on multiple tables (for example you might have the name columns on a Supplier table).

Upvotes: 15

Perception
Perception

Reputation: 80603

Its the difference between composition and aggregation. @Embedded objects are always managed within the lifecycle of their parents. If the parent is updated or deleted, they are updated or deleted as well. @OneToOne objects may mimic composition via the cascadeType option of their @Join annotation, but by default they are aggregated, aka their lifecycle is separate from that of their parent objects.

Upvotes: 12

Related Questions