Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between @Entity and @Embeddable

The difference between @Entity and @Embeddable annotation when each one is added before class declaration?

  1. the first create class as an entity, second insert column from another table?
  2. the first create class as an table, while second is embedded in another class?
  3. the first sets standard as a class, second define table type
  4. the first create table for that class, second embed something into different class
  5. the first define table property, second create union of two tables
like image 330
Sanjeev Yadav Avatar asked Jan 15 '14 17:01

Sanjeev Yadav


People also ask

What is @embeddable used for?

@Embedded / @Embeddable allows us to easily do this without having to split the database table. Second, it allows for reuse of common mappings between entities. Say each table has a simple revision tracking, with two columns containing the username of the person who changed the row, and the time it happened.

What is the @entity annotation used for?

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.

What is the purpose of @entity in JPA?

An entity can aggregate objects together and effectively persist data and related objects using the transactional, security, and concurrency services of a JPA persistence provider.

What is @embeddable 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.


2 Answers

@Entity annotation over a class defines that, it has a distinct separate existence. Thus we can run DB queries, without being dependent on any other class. @Embeddable annotation over a class defines that, it does not have independent existence. Thus we cannot run DB queries, without depending on other class. Here is an example to understand it better:

@Entity User   -- long id   -- String name   -- String email      @Embedded   -- UserDetails userDetail  @Embeddable UserDetails   -- Date dateOfBirth   -- String sex   -- String address   -- String maritalStatus 

Here you can see without having a User, UserDetails is useless.

Generally, in OOP, we first design the classes and then we design database entities. For some classes (like UserDetails class in the above example), we do not want to have separate tables in DB, where their independent existence is meaningless. In those cases, we mark the class as embeddable.

Typically, embeddable classes share the same table as the Entity in which they are embedded

like image 63
Sazzadur Rahaman Avatar answered Sep 22 '22 13:09

Sazzadur Rahaman


Entities have an identity and can be queried for. Embeddables have no identity of their own and can only be queried for using the owning entities.

If you open an entity class, you will always find the @Id annotation - it is mandatory. If you open an embeddable class, you will never find an @Id annotation - it is forbidden.

EDIT: It is not entirely correct that embeddables can only be stored as a part of the parent, i.e. in the same table. This is only true for one-to-one relationships. You can have Collections and Maps of embeddable objects in the parent entity and they will be mapped to own collection tables.

like image 38
kostja Avatar answered Sep 21 '22 13:09

kostja