What is difference between @Entity and @Embeddable
Asked Answered
D

5

30

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
Disillusionize answered 15/1, 2014 at 17:4 Comment(0)
A
63

@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

Appalachian answered 15/1, 2014 at 17:11 Comment(2)
Embeddable components can have their own database tables in case you are using them as part of a collection like a bag, set, list and map (one to many relationship when 1 entity has many embeddable component instances).Ostracism
right @aladin and dont forget the important example of many to many with a join table that has more attributes than just the two FKs. That is when you have to do this mkyong.com/hibernate/…Lanita
H
21

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.

Hydromel answered 16/1, 2014 at 8:21 Comment(0)
I
6

An entity class is an ordinary user defined Java class whose instances can be stored in the database.

@Entity
@Table(name="dog")
public class Dog{
    @Id
    @Column(name = "id")
    private int id;

    @Embedded
    private Animal animal;
    public Dog(int id,Animal animal){
        this.id=id;
        this.animal=animal;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Animal getAnimal() {
        return animal;
    }
    public void setAnimal(Animal animal) {
        this.animal = animal;
    }
}

Embeddable classes are user defined persistable classes that function as value types. As with other non entity types, instances of an embeddable class can only be stored in the database as embedded objects, i.e. as part of a containing entity object.

@Embeddable
public class Animal {

    @Column(name = "name")
    private String name;

    @Column(name = "location")
    private String location;
    public Animal(){
    }
    public Animal(String name,String location){
        this.name=name;
        this.location=location;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
}
Ingenious answered 16/1, 2014 at 16:8 Comment(0)
L
3

It is an old topic but I would like to add my answer, which is more from theoretical point of view. In DDD (domain driven design) we usually have Entity and Value Objects. The first ones are identifiable only by a an identity that they have. The second ones are not defined by an identity, which means that if all the components that make that particular objects are the same, than the 2 value objects are the same.

The analogy is that in this case, if we were to apply DDD, the Entity is the class annotated with @Entity and the Value Object is the one with @Embeddable. A demonstration of this is the fact that the embeddable object is added as addditional information to an existing record, which already has its own identity defined externally to the embedded object.

Laurynlausanne answered 27/11, 2016 at 16:42 Comment(0)
B
0

Well @Entity signifies that the entity object has significance all by itself it doesn't require any further association with any other object. Where as @Embeddable object doesn't carry any significance all by itself, it needs association with some other object.

Lets take an example of say i have a Employee Object and it has a collection of Address Object as its member variable. Now when when speak of any address we need to tell whose address it is, which employees address it is. If we just talk about the address it doesn't make any sense. Hope this gives you the difference between the two.

Brady answered 2/5, 2019 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.