In Hibernate, mapping types categorized into two groups. In this article we will see overview of Hibernate mapping types and the difference between them.
Hibernate mapping types
Hibernate categorized into two groups with respect to persistence.
- Entity Type
- Value Type
Entity Type
An object of Entity
type has its own database identity means primary
key
in table.
An entity has its own lifecycle. It may exist independently of any other entity.
A reference of entity object is persisted as a reference in database (foreign key).
Value Type
An object of Value
type has no database identity and it belongs to entity.
The lifecycle of Value
type object is bounds to its owning entity
object's lifecycle.
Value
type object is identified by its owning entity
.
It is embedded into owning entity and it represented as table's column in database./p>
Value
types further divided into three groups.
- Basic value types
- Composite types
- Collection types
Basic value types
- Basic value types maps a single database value (column) to a single, non-aggregated Java type.
- Hibernate provides a number of built-in basic types and all the Java wrapper classes.
String
,Character
,Boolean
,Byte
,Short
,Integer
,Long
,Float
,Double
,BigInteger
,BigDecimal
,java.util.Date or java.sql.Timestamp
,java.sql.Time
,java.sql.Date
,java.util.Calendar
,java.util.Currency
,java.util.UUID
,java.lang.Class
,java.sql.Blob
.. etc are built-in basic value types.- You can find the all the built-in basic types from Hibernate documentation Basic value types.
Composite types
- The Java Persistence API calls these
embedded types
, while Hibernate traditionally called themcomponents
. - Components represent aggregations of values into a single Java type.
- In many ways, Composite types looks exactly like an
entity
.Composite types
does not own its own lifecycle nor does it define an identifier.
Collection types
Hibernate allows to persist collections. Collections types
further sub categorized
into three types.
- Collection of basic value types.
- Collection of Composite or embeddable types.
- Collection of custom types.
Now we take one example to understand the Entity type
, and Value Type
.
Below USERS table has user details, home address, and billing address.
Now we a represent the same table in the object model like below with the six-member variables homeStreet, homeCity, homePincode, billingStreet, billingCity, and billingPincode.
public class User { private Long id; private String name; private String homeStreet; private String homeCity; private String homePincode; private String billingStreet; private String billingCity; private String billingPincode; }
Rather we would like to have a separate class User and Address. Address class only have address related details. The homeAddress and billingAddress member variable of class User to access user's address from it. This is better design from code reuse perspective.
public class User { private Long id; private String name; private Address homeAddress; private Address billingAddress; }
public class Address { private String street; private String city; private String pincode; }
The id attribute in User
class identity the record uniquely in database's
table. Here we don't have id attribute in Address
class and didn't have any
significance of identity of an address.
You can follow the article this article to create Hello World application with Hibernate. and Hello world application using JPA Annotations. You can also follow this article to find out the difference between @Entity and @Table annotation.
The objects that have their own database identity are Entity type
object. So here
User
is an Entity type
and Address
is a Value
type
.
No comments :
Post a Comment