Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@embeddable vs @entity for a mapping a collection

This must be quite naive but I have a doubt on when to use @Entity and @Embeddable.

Say I have a User and Notification class.

 @Entity
 public class User{
     //other properties
     @onetomany
     private List<Notification> notifications;
 }

 @Entity
 public class Notification{
     //properties
 }

I understand that there will be tables for class User and Notification, and a third table for mapping. What if I do it like this?

 @Entity
 public class User {
     //other properties
     @ElementCollection
     private List<Notification> notifications;
 }

 @Embeddable
 public class Notification{
     //properties
 }

I know this won't create a table for Notification. But I can still store my notification objects. I went through the documentation, but couple of doubts:

  1. Is it based on whether I want to see class B as a seperate table?
  2. Is there a performance difference b/w creating a table and an embeddable object?
  3. What can I not do with embeddable object that I can do with a table other than directly querying the table?

NOTES

For anyone reading this question, this question too might help you.

like image 593
shazinltc Avatar asked Oct 04 '12 06:10

shazinltc


People also ask

What is the use of @embeddable?

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

How do you map a table and entity?

In Spring Data JPA we can map an entity to a specific table by using @Table annotation where we can specify schema and name. But Spring Data JDBC uses a NamingStrategy to map an entity to a table name by converting the entities class name.

What is an embedded entity?

Embedded entity means an entity that. has a direct or indirect interest, as a. stockholder, member, beneficiary, or. otherwise, in another entity.

Is it possible to create an embeddable entity in same table?

Yes, when you use @Embedded, You embed that @Embeddable entity in @Entity class, which makes it to add columns for embedded entity in same table of @Entity class. Is there a performance difference b/w creating a table and an embeddable object?

Do we need to use @embedded annotation in Entity Framework?

At owning Entity (User) side not require to use @Embedded annotation, because Embeddable data stored in separate collection table. Line numbers 23,24 from below code snippet describes the mapping the Collection of Embeddables. Specifies the table that is used for the mapping of collections of basic or embeddable types.

Is there a performance difference between @embedded and @entity class?

Yes, when you use @Embedded, You embed that @Embeddable entity in @Entity class, which makes it to add columns for embedded entity in same table of @Entity class. Is there a performance difference b/w creating a table and an embeddable object? When you use @Embedded, for table creation, one query is required, also for inserting and selecting a row.

What is the difference between @elementcollection and @embedded annotation?

@ElementCollection annotation is used to map either Basic Value type or Composite Value type. At owning Entity (User) side not require to use @Embedded annotation, because Embeddable data stored in separate collection table.


1 Answers

  1. Is it based on whether I want to see class B as a separate table?

Yes, when you use @Embedded, You embed that @Embeddable entity in @Entity class, which makes it to add columns for embedded entity in same table of @Entity class.

  1. Is there a performance difference b/w creating a table and an embeddable object?

When you use @Embedded, for table creation, one query is required, also for inserting and selecting a row. But if you don't use it, multiple queries are required, hence, use of @Embedded yields more performance, we can say.

  1. What can I not do with embeddable object that I can do with a table other than directly querying the table?

Removing the respective embedded entity may be, but there may be integrity constraint violations for this.

like image 124
deepakraut Avatar answered Oct 17 '22 21:10

deepakraut