Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 973
Harmeet Singh Taara Avatar asked Dec 05 '12 13:12

Harmeet Singh Taara


People also ask

What is embedded annotation in hibernate?

With Hibernate we can use the @Embeddable annotation to mark a class to be eligible as an embeddable class. We can use the @Embedded annotation to embed an embeddable class. The attributes of an embedded class can be overridden using the @AttributeOverrides and the @AttributeOverride annotations.

What is the use of @ID annotation?

Simply, @Id: This annotation specifies the primary key of the entity. @GeneratedValue: This annotation is used to specify the primary key generation strategy to use. i.e Instructs database to generate a value for this field automatically. If the strategy is not specified by default AUTO will be used.

What is @basic annotation in hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.


5 Answers

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

like image 59
Ben Thurley Avatar answered Nov 03 '22 13:11

Ben Thurley


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.

like image 35
Perception Avatar answered Nov 03 '22 14:11

Perception


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

like image 39
Inder Avatar answered Nov 03 '22 14:11

Inder


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
like image 30
CourseTriangle Avatar answered Nov 03 '22 13:11

CourseTriangle


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

like image 28
Dimitri Dewaele Avatar answered Nov 03 '22 13:11

Dimitri Dewaele