Sunday, March 3, 2013

Hibernate Course: Section 4 Basic Mapping Annotation V17 - V26

Vide 16: Hibernate & JPA
we should know that JPA is just an interface, and hibernate implements this interface.
However, hibernate went beyond what we have in JPA by adding new annotation and new functionalities.
you should know that if you use the hibernate features you will be locked by hibernate.

all JPA annotations are under javax.persistence, the new hibernate annotations are under org.hibernate.annotations


Video 17: Field vs Property Access
when you annotate your class, you can do this:


as you can see here we are annotating the field

however, we can do another thing which is annotating the get method not the field like this

as you can see we removed the annotation from the field to the get methods,

you can also use @Access(value=AccessType.PROPERTY) on the class level, it is a good practice to alwayse use it.

the question is when to use property or field access?
so in field access, you are allowing Hibernate to access private fields, which violate the class encapsulation, sure hibernate uses reflection to access these values.
when you use property access, you can add some extra logic to your get methods.

choose what suits you best.

Video 19: @Column
some of the important attributes for @Column
1- updatable=false: it means that don't add this column to the update statement when we do an update, for example in this case we should not update the createdDate
2- insirtable=false: same as above but for insert
3- nullable=false: which means the field cannot be null.

let's see that birthdate field is defined in the database as cannot be null, and we don't set nullable = false in the User class. If we insert a record with birthdate = null, THE DATABASE will throw an exception, however if we set nullable = false then HIBERNATE will throw the exception. it is better to let Hibernate throws exceptions not database.

Video 20 & 21: Identifier
in this tutorial he talked about comparing records, you know that we can compare using == to compare instances and equals() to compare values.

and as you know we have to types of primary keys in the database
1-  surrogate key: which means an id column
2- natural key: a key that is a field or multiple field in the database

the preferred key for hibernate is surrogate key,

we saw that you define the surrogate key by
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)

the @GeneratedValue defines how we set the value of new records, when we set it as IDENTITY, it means let the underling database decide how to generate this value

in case of using ORACLE, we use sequences for generated values, in order to map that to oracle we write



 as you can see we use @SequenceGenerator

another way to generate this value is to use just
@GeneratedValue

without anything, this way Is called auto generator, the auto generator means let the persistence engine decides the best way to generate value based on the underling database

another way is called TableGenerator, which means using a specific table to generate the key value, we define a table with just two columns The Key Name and The Value
then we write


as you can see we use @TableGenerator tag

Video 22: @Transient
lets say you want to add a field to the User class,
private Boolean valid;

even if you don't add annotation to this field, hibernate will try to insert this field into the database, however the valid field is not part of the database we use it for different purposes,
we should tell hibernate to ignore this field when it does any database operations, that is why we use the @Transient

@Transient
private Boolean valid;

video 23: @Temporal
normally, databases provides you with 3 date types,
DateTime: to save date and time
Date: to save just the date
Time: to save the just the time

MySQL provides you with another type which is Timestamp, which is somehow similar to datetime.

in order to map these fields to java object, you can use java.util.Date

@Column(name = "DATETIME_COLUMN")
 private Date datetimeColumn;

 @Column(name = "TIMESTAMP_COLUMN")
 private Date timestampColumn;

 @Column(name = "DATE_COLUMN")
 private Date dateColumn;

 @Column(name = "TIME_COLUMN")
 private Date timeColumn;

however, if you check the value of these dates, you will find that java.util.Date will always have a date and time values even though the field could be of type Date (only Date) or time (only Time).

in order to fix this problem we use the @Temporal tag
@Temporal(TemporalType.TIMESTAMP)
 @Column(name = "DATETIME_COLUMN")
 private Date datetimeColumn;

 @Temporal(TemporalType.TIMESTAMP)
 @Column(name = "TIMESTAMP_COLUMN")
 private Date timestampColumn;

 @Temporal(TemporalType.DATE)
 @Column(name = "DATE_COLUMN")
 private Date dateColumn;

 @Temporal(TemporalType.TIME)
 @Column(name = "TIME_COLUMN")
 private Date timeColumn;

@Temporal will till java to store the correct value.

Note1: it is important to use @Temporal, otherwise you will have some casting issue when you store to datebase.
Note2: you should use @Temporal with java.util.date, or with Calender if you are using Calender
Note3: there are other types which solve the issue like:

Video 24: @Formula
@Formula is a Hibernate annotation

it is used like this
you write a formula to do the calculation of the field
@Formula works just when you select the record it has nothing to do with insert or update,

Lecture 25 & 26 Basic Data Types

IN hibernate we should distinguish between 2 data types:
1-Entity Type
2-Value Types

Entity Type is a record in the database, it has a life cycle, for example User is an entity type when you write
User x = new User()
the object x still not persistence, it is in the transient state,
when you do save(x), now x is in persistence state.

On the other hand Value types DONT HAVE LIFE CYCLE, for example String firstName, doesn't have life cycle its life cycle is owned by the User class.

we have different Value Types:
1- Basic Types: like String firstName:
2- Composite Types: like Address in the image, Address doesn't have a table in the database, it is just a way to organize the object, Address fields are simply columns in the User table.
3- Collection Value Types: like List<String>

you can check online how Hibernate maps the Basic types
http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/types.html






No comments:

Post a Comment