Why do we use @Embeddable In Hibernate
Asked Answered
E

4

64

What's the use of @Embedded and @Embeddable In Hibernate ? Because every example i found on internet is inserting data inside of a single table and to do that using two different class. My point is if I am using a single table then I can map all the columns inside of a single class then why should i use different class. and if We use two different table then there is one-to-one and one-to-many hibernate relationship.

Erythrocytometer answered 13/10, 2013 at 4:23 Comment(0)
R
115

There are two types of objects in Hibernate
1. Value Object
2. Entities

Value Objects are the objects which can not stand alone. Take Address, for example. If you say address, people will ask whose address is this. So it can not stand alone.

Entity Objects are those who can stand alone like College and Student.

So in case of value objects preferred way is to Embed them into an entity object.

To answer why we are creating two different classes: first of all, it's a OOPS concept that you should have loose coupling and high cohesion among classes. That means you should create classes for specialized purpose only. For example, your Student class should only have the info related to Student.

Second point is that by creating different classes you promote re-usability.

When we define the value object for the entity class we use @Embeddable.
When we use value type object in entity class we use @Embedded

Rena answered 13/10, 2013 at 4:29 Comment(6)
now I understand the logic for creating two different class, I was thinking Embedded and Embeddable is only applied for a single table? or it can be used for multiple tablesErythrocytometer
I have never tried but I believe we can use, as I cannot see any direct harm into itRena
Dependancy on other objects is only an indicator not an evidence. For example Addresses could be reused and modeled as entitys or compositions/aggregations in general.Local
Is it value or entity if a class implements org.hibernate.usertype.UserType with two String fields?Opaque
@Opaque An attribute using a custom UserType would be considered a value type, specifically a "basic" type. See docs.jboss.org/hibernate/orm/current/userguide/html_single/…Rapper
Can an Entity be embedded in another Entity?Succentor
A
5

suppose we have employee table annotated with @entity and employee has Address so here i dont want to create two tables i.e employee and address, i just want create only one table i.e employee not Address table then we need to declare Address instance in Employee and add @embedable annotation on top of Address class, so finally we get table employee with its record and address records as well in single employee table

Ahimsa answered 11/7, 2019 at 15:48 Comment(0)
F
0

This method is only for coordinating the difference between the business layer and the database

Feudalism answered 11/1 at 22:54 Comment(0)
B
-1

One entity can be embedded in another entity. The attributes of an entity can be common attributes of more than one entity. In this case there can be one embeddable entity. And this embeddable entity can be embedded in more than one entity.

Let's consider an example. We have one Animal entity, which has name and location attributes. Now two different entities Lion and Elephant can have Animal attributes just by embedding the Animal entity. We can override the attributes. In Animal entity there is location attribute and in Elephant there is place attribute. So with the help of @AttributeOverrides we can do like below:

@AttributeOverrides({ @AttributeOverride(name = "location", column = @Column(name = "place")) })
Bookbinder answered 1/5, 2014 at 13:9 Comment(1)
This seems like a paraphase of concretepage.com/hibernate/…. If yes, I think you should add a reference to that article or somehow mark it as a quotation.Fighterbomber

© 2022 - 2024 — McMap. All rights reserved.