@embeddable vs @entity for a mapping a collection
- 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.
- 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.
- 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.
Related videos on Youtube
Comments
-
shazinltc about 1 year
This must be quite naive but I have a doubt on when to use
@Entity
and@Embeddable
.Say I have a
User
andNotification
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
andNotification
, 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:- Is it based on whether I want to see class B as a seperate table?
- Is there a performance difference b/w creating a table and an embeddable object?
- 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.
-
shazinltc about 11 yearsI suppose I can use Embeddable with ElementCollection. I need not use @Embedded rt?
-
deepakraut about 11 years3rd point: consider Person contains Address (embedded), if there is separate table for both, You can directly remove associated address, but if Person's table has F.K. to address, it may cause integrity constraint violation.
-
deepakraut about 11 years'@Embeddable' makes the class eligible for embedding. '@Embedded' actually embeds the embeddable entity.
-
deepakraut about 11 yearsen.wikibooks.org/wiki/Java_Persistence/Embeddables this gives nice explanation for embeddable and embedded
-
shazinltc about 11 yearsThanks a lot.. :) It did help :)