Hibernate
1) An ORM Tool.
Class corresponds to a table and an Object corresponds to a row in the table.
2) Used in the
data layer of applications
3) Implements
JPA (Java Persistence API)
4) In short,
provides a complete solution to implement DAO layer of your Java or JEE
application.
Problems:
1) Mapping
member variables to the column.
2) Mapping
relationships
3) Handling
data types.
4) Managing
changes to object state.
To connect
with hibernate, we need to
1) Include
Hibernate jars and database jdbc jars
What we
need to save an Object
1) JDBC
Database – Hibernate configuration
2) Model
Object - Annotations
3) Service
method to create the model object- Hibernate API
4) Database
design- Not needed
5) DAO method
to save object using SQL queries- Not needed
Hibernate
API
1) Create a Session factory – 1 object per
application
2) Create a
Session from the session factory
3) Use the
Session to save model objects
Annotations
used
@Entity(name="USER_DETAILS")
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
IDENTITY-use identity feature of
DB
SEQUENCE- Use hibernate sequence
object
TABLE-separate table for
generating primary key
@Column(name="USER_ID")
@Transient – Ignore
field .No column will be created by Hibernate
@Temporal(TemporatlType.DATE)
– Ignore timestamp, only store the Date
@Lob – Large
objects. Telling hibernate that String field is very large, Examle Description
Natural and Surrogate Key
Serial number
column, userId not serving any purpose is surrogate key. Hibernate can do the
job for us.(Unique and mandatory )
Email ID address is
helping to uniquely distinguish the data is primary key or natural key
Value Object and Entity
Entity has meaning
on its own. Thus when it comes to Entity associated with another Entity, new
table is created.
Address object
doesn’t have meaning by alone, thus it’s a Value Object. No new table is
created and address fields are embedded in same table.
@Embeddable
– Embed value object to the same
Entity. No new table creates.
Proxy Objects in hibernate
1) Proxy User Class is a dynamic subset of Original User
Class.
2) When session.get(User.class,1) is
called, hibernate returns Proxy User class which pulls only first level fields
from DB . This is called lazy initialisation as hibernate isn’t returning
Addresses in this case.
3) When user.getListOfAddresses()
is called, hibernate gives call to Hibernate and fetches all the addresses.
No comments:
Post a Comment