Sunday, March 10, 2013

Hibernate Course: Section: 11- Criteria API v78 - v81

simple criteria like ordering


and you can do the same in JPA


Less than or equal criteria 

and in JPA


Pagination in Hibernate

pagination in JPA would be


Saturday, March 9, 2013

Hibernate Course: Section: 10 - Hibernate Query Language and Java Persistence Query Language v70 - v77

Writing Queries
you can write a query in hibernate like this:



in hibernate you can create like this



Expressions and Operators
in hibernate


just one thing when you use "t" in the example, you are using an object from transaction so you can write t.amount or go to another level for example t.amount.SOMETHING

in JPA



Parameters


another example



Joins



as you can see we have a normal join here

we have another thing which is called implecit join


as you can see there is no join there, however when you say select t.account, hibernate knows that we should do this join.

ALWAYSE USE THE EXPLICIT JOIN

Functions
sure you can use functions in your query


Named Query
sometimes you find yourself writing the same query multiple times, thats why we have named query, you define the query one time and then use it


then you can use it

Lazy Loading
you know that for relations we have eager or lazy fetching, to set the value and say if it is lazy or eager


Friday, March 8, 2013

Hibernate Course: Section: 9 - Advanced Mapping and Configuration v59 - v69

Compound Primary Key
it is always better to use surrogate keys (i.e ID field).
however sometimes we need a compound keys:

we have this example
Currency table has 3 columns (CurrencyName, CountryName, Symbol).
the key is CurrencyName & CountryName (e.g. Dollar,US or Dollar,Canada).

to represent that:

1-

you can see, we use @Id for both fields, and we use IdClass(CurrencyId.class).

2- we define the IdClass

as you can see we defined the CurrencyId class, which implements serializable
THE FIELDS (name, countryName) MUST MATCH THE FIELDS NAME IN Currency class.


that is it, now if you want to use this key your wirte


as you can see, we use the CurrencyId object to get an object from database.

Compound Join Column
so now we have the Currency class, lets say we want to have a one to many relationship with this class.

how can we do the Join with a table where the table has 2 columns as a primary key
as you can see we use @JoinColumns and inside it set both columns

Enumaration
lets say you have this enum:


and you want to save this information in the table



Simply use @Enumerated

Mapped Superclass Inheritance 
lets say you have this data model
Stock (Stock_id, Name, Issuer, Purchase_date, Share_price, Quantity)
Bond (Bond _id, Name, Issuer, Purchase_date, Interest_Rate, Maturity_date)

so as you can see we have (Name, Issuer, Purchase_date) in both table, so we will abstract them and use inheritance when we define the entities:

this is the base class, notice the use of @MappedSuperclass


and here is the bond entity extending this class


IMPORTANT: when you are @MappedSuperClass, you dont have persistence life cycle, which means you cant have something like one to many relation with the Investment because it is not an entity, it is just heirachal thing


Table per class inheritance
to solve the issue in the previous example, we should not use @MappedSuperClass, we should use @Inheritance:

@Inheritance has 3 different mode, the first one is table per class, which means we should have a table for each class Bond, Stock and Investment ( the abstract class ).


we will not continue in the example the main idea is @Inheritance and the strategy is TABLE_PER_CLASS

in this TABLE_PER_CLASS, hibernate will do a UNION query to union the tables, this is a big drawback

Single Table Inheritance
in a single table inheritance, we will have only one table INVESTMENT, it has all the shared and unshared fields, we will distinguish between BOND and STOCK by adding what we call a discriminator field.


as you can see we defined the DiscrminatorColumn

now in Bond we do this

as you can see we removed the @Table annotation because Bond is not a table anymore

Building a Persistence Layer
it is better to have a package for Data.

start by a general DAO interface


Here you have the general operations that can be run on any table, find save delete

then implement this interface in AbstractDAO
basically you will implement all the methods in the DAO interface and we will add another methods that are generic for all interfaces



now we will have an DAO for each entity, so lets say user DAO


as you can see UserDAO extends DAO, so it will provide all the methods in DAO plus extra methods related to User, which is findByFirstName.

now we should implement this interface


as you can see we extednd the AbstractDAO to get all the general functionalities, and we implement the UserDAO functions.

so now if you have BANK, you create a BANK DAO interface then you implement it.

Views for COmplex Queries
sometimes you may have a complex query which you cannot do it in hibernate, in this case it is better to create a View in the database then you can simply map it in Hibernate:

Schema Generation
you can generate the tables based on the entities you defined, however this is something not for production, it is better just to do it for testing


as you can see, we should add this to the hibernate.cfg.xml

create means: create the tables
we have other options
validate: means check that the defined entities follow the tables structure we have in the database
update: if there is any new field for example it will be added
create-drop: means create the tables do everything then after we finish it will drop everything (which means when the program exits you will have a clean schema)

Thursday, March 7, 2013

Hibernate Course: Section:8 - Hibernate API V51 - V58

In the previous section we talked about Hibernate API, as we know Hibernate is the implentation of JPA APIs, Hibernate added some extra APIs.0


we have Session in Hibernate and EntityManager in JPA.
and you can see in the table above the methods mapping.


In this section we will talk about JPA APIs

JPA Configuration
add persistence.xml , this file should be inside META-INF
add orb.hibernate dependency.


as you can see, in persistence.xml, you set the provider, in our case it is hibernate, which means we will use hibernate implementation for JPA. (other possible provider is eclipse links).

we dont have to specify the Entities we have here like what we were doing with hibernate.xml, the application will go over the classes in the class path and detect all the classes that are annotated with @Entity.

JPA Saving Entities

as you can see we have EntityManagerFactory in JPA which is similar to SesssionFactory in Hibernate.
we have EntityManager which is similar to Hibernate Session
we have EntityTransaction which is similar to Hibernate Transaction.

as you can see we use persist() in JPA to save to database

another thing "infinite-finance" is the persistence-unit name that is defined in the xml file


JPA Retreiving Entities

find() is similar to get() in hibernate.


getRegerence() is similar to load() in Hibernate.

JPA Modifying Entity 

here we do find()
then change the object
then on commit() the record will be updated

JPA Removing Entity

we use remove() which is similar to delete() in Hibernate

JPA Reattache 

we use detach() to detach, and merge() to attach again.


Wednesday, March 6, 2013

Hibernate Course: Section: 7 - Hibernate API V41 - V50

in hibernate the life cycle is so simple
The object can be Transient,Persistent, Removed or detached.

if you are Persistent or Removed you are in the Persistence Context.

Transient: when you create a normal POJO from an Entity you are in transient mode, so for example 
Bank x = new Bank(); this is a Transient object, it is not associated with a database record, 

Persistent: it means the entity (the object) is associated with a database record, the object here is in the Persistence Context. if you do any manipulation on this entity, this manipulation will be reflected in the DB as well.

Removed: you are in the persistence context and scheduled for deletion.

Detached: if you are an object in the persitent state however we close the Persistence Context ==> you will be in the Detached state.

What is a persistence context
Persistence Context is an object that provides us with a place to put all the entities which are in the persistent state. All the changes that we do on these entities will be in the Persistence Context and will be sync with the DB later.
We get a Persistence Context by using a session
when you openSession() you are creating a new persistence context.

Hibernate API

if you get() or load() from the database you will be in the Persistent state.
you can save() or saveOrUpdate() to move from Transient to Persistent
you can call delete() on a persistent entity to make it removed.
if the persistence context is closed the entity will be detached, if you still have a reference to that entity, you can attach it again and make it persistent by calling update() or saveOrUpdate().

also you can detach an entity (without closing the Persistence Context) by calling the evict() method.

here is an example

as you can see here after the bank was detached we called
session2.delete(bank), YOU CANNOT DO THAT IN JPA THIS IS SOMETHING ONLY IN HIBERNATE

Saving Entities

as you can see, firstly we openSession() (i.e create a Persitence Context).
then we create account, trans1 and trans2 (those are Transient Entitiy now).

then we do a simple check session.contains(account) to check if these entities are in the PersistenceContxt, of course will return FALSE.

then we begin the transaction, 
session.save(account) // now account is in Persistence state.

then we commit() which flushes the data to the database.
then we close() the context


Retrieving Entities from DB

we use get(CLASS TYPE, ID), as you can see get() executes a select statement

another example:

as you can see 2 get() and one select statment, the first get runs a select statmnet, the net get() for the same ID will get the result from the cache of the session.

another method to retrieve is load()
as you can see, load() is lazy, the select statment will run not when you do session.load(), it will run when you do bank.getName().

Modifying entities

as you can see the update statement runs when we do commit().

Removing Entities:



as you can see we get() --> select statment
then we check session.contains()--> true, the object is in the session.
then we delete()--> as you can see the delete() will not generate a delete statment to the database, the record will be delete when we flush(i.e. when we commit).
then we check session.contains() again --> false because the object is detached.
then transaction.comit()--> delete statment, we can see 2 delete statment because there is a cascade.ALL.

VERY IMPORTANT: after the bank object is deleted it should not be used anymore and it should be garbage collected because it is now deleted from the DB.

Reattaching detached object.
 we do that with update() or saveOrUpdate() methods

Flushing
the flush will happen if you call flush() or transaction.commit() or sometimes when we do select, a flush may occur