• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Embeddable classes

 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we need to use Embeddable classes for entities?

I've heard that the purpose of using Embeddable classes is, they can be used as 'types' for persistence variables. Is this the only usage of Embeddable classes?

I've also heard in addition to Embeddable classes, we can use any Serializable type for persistence variable. If we can any Serializable type, then what is the usage of Embeddable classes?
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone answer this question, please?
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use embeddable when you have a class that does not have its own table or primary key. It exists inside the table that another class is mapped to. This is the equivalent of the "component" mapping in Hibernate (also known as a Value Type).
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javid Jamae wrote:You use embeddable when you have a class that does not have its own table or primary key. It exists inside the table that another class is mapped to. This is the equivalent of the "component" mapping in Hibernate (also known as a Value Type).


Thank you Javid, but I didn't yet understand! We can use any Serializable type, instead of an Embeddable type, isn't it? So, why Embeddable here....?
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can use any Serializable type, instead of an Embeddable type, isn't it?

I'm don't quite understand what you mean by this. Could you provide a link to what you're talking about?

If it helps, you may not want to persist serialized data, you may want to persist the actual columns in the embeddable class in the same table. This is like having a one-to-one relationship between two objects, one being an entity type and one being a value type, and both are mapped to the table where the entity type points.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Javid, look at this:




But, it BidDetails (pojo) class impements Serializable interface, we can write it, without using @Embedded or @Embeddable annotations as...



Am I correct on this?
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have an environment ready to try it out right now. Did you try it both ways? Did it work the second way? I suppose if it works, that it is automatically detecting that BidDetails has some property mappings that exist in the table for the Bid and it just works. If so, that's nice, but I would probably put the annotation on there anyways just so the relationship is a little more clear.

Side note: Using plurals for class names (like BidDetails) is usually not good naming convention. I'd suggest BidDetail instead, since a collection of those would be bidDetails.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javid Jamae wrote:I don't have an environment ready to try it out right now.

Me too .



Javid Jamae wrote:
Side note: Using plurals for class names (like BidDetails) is usually not good naming convention. I'd suggest BidDetail instead, since a collection of those would be bidDetails.


Hay Javid, thank you very much on this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic