Before jumping to @Embeddable and @Embedded annotation. Let me explain about hibernate different objects:
Entity Object
o Entity object are those object which can stand alone like Student or Professor and has its own database identity.
Value Object
o Objects which cannot stand alone like Address as you need to map address with some Entities like Student. It will belongs to an entity, and its persistent state is embedded in the table row of the owning entity
In short, always use @Embeddable for the value object and @Embedded with the entity class.
Let's understand it by a simple example:
We have one Address (Value object) and it is having attributes like city, state, zip code. Now we have two more different entity Student and Professor (Entity Object). Student or Professor can have Address attributes just by embedding the Address into its Entity.
The @Embedded annotation is used to specify the Address entity should be stored in the STUDENT table as a component.
@Embeddable annotation is used to specify the Address class will be used as a component. The Address class cannot have a primary key of its own, it uses the enclosing class primary key.
Download the source code of this example from Github.
You will see the output once you execute the Application class from source code
and will create the following tables in the database
Happy Coding..!!!
No comments:
Post a Comment