@Embeddable and @Embedded Annotation – Hibernate

@Embeddable and @Embedded Annotation – Hibernate
We have seen some basic annotations of hibernate (here), Now we are going to see about @Embeddable and @Embedded annotations.

One entity can be embedded into another entity, One entity attributes may be common to more than one entity.

Let us understand with an example,
Consider we have 2 pojo’s – employee and manager. We create another pojo named address. Here address attribute is common for both employee and manager. Address has common attributes that are required by both these java classes. @Embeddable and @Embedded annotations play a major role in embedding address into employee and manager entities.

Lets see @Embeddable and @Embedded example for employee entity.

Structure
structure

address.java

employee.java

NewHibernateUtil.java

hibernate.cfg.xml

Output
op1

op2

Note:
If you like to change the column name of address.java, use the same annotations of employee.java – @Column (name=””)

 

 

One thought on “@Embeddable and @Embedded Annotation – Hibernate

Leave a Reply

Your email address will not be published. Required fields are marked *