Interview With Gavin King:-
----------------------------------------------------------------------------------------------------------------------
Whether the java application will run in the server or without server,
and the application may be desktop or stand alone, swing, awt, servlet…what ever,
but the steps are common to all.In order to work with hibernate we don’t required any
server as mandatory but we need hibernate software (.jar(s) files).
Follow The Steps:
1.Import the hibernate API, they are many more, but these 2 are more than enough…
import org.hibernate.*;
import org.hibernate.cfg.*;
2. Among Configuration, Mapping xml files, first we need to load configuration xml,
because once we load the configuration file, automatically mapping file will be loaded as we registered
this mapping xml in the configuration file. So to load configuration xml, we need to create object of Configuration class, which is given in org.hibernate.cfg.*; and we need to call configure() method in that class, by passing xml configuration file name as parameter.
Eg:
Configuration cf = new Configuration();
cf.configure(“hibernate.cfg.xml”);
Here our configuration file name is your choice, but by default am have been given hibernate.cfg.xml, so once this configuration file is loaded in our java app, then we can say that hibernate environment is started in our program.
So once we write the line_ cf.configure(“hibernate.cfg.xml”), configuration object cf will reads this xml file hibernate.cfg.xml, actually internally cf will uses DOM parsers to read the file.
Finally…
cf will reads data from hibernate.cfg.xml
Stores the data in different variables
And finally all these variables are grouped and create one high level hibernate object we can call as SessionFactory object.
So Configuration class only can create this SessionFactory object
likeSessionFactory sf = cf.buildSessionFactory();
Actually SessionFactory is an interface not a class, and SessionFactoryImpl is the implimented class for SessionFactory, so we are internally creating object of SessionFactoryImpl class and storing in the interface reference, so this SessionFactory object sf contains all the data regarding the configuation file so we can call sf as heavy weight object.
3. Creating an object of session,
Session is an interface and SessionImpl is implemented class, both are given in org.hibernate.*;
When ever session is opened then internally a database connection will be opened, in order to get a session or open a session we need to call openSession() method in SessionFactory, it means SessionFactory produces sessions.
Session session = sf.openSession();
sf = SessfionFactory object
4. Create a logical transaction
While working with insert, update, delete, operations from an hibernate application onto the database then hibernate needs a logical Transaction, if we are selecting an object from the database then we do not require any logical transaction in hibernate. In order to begin a logical transaction in hibernate then we need to call a method beginTransaction() given by Session Interface.
Transaction tx = session.beginTransaction();
session is the object of Session Interface
5. Use the methods given by Session Interface, to move the objects from application to database and from database to application
session .save(s) - Inserting object ‘s’ into database
session.update(s) - Updating object ‘s’ in the database
session.load(s) - Selecting objcet ‘s’ object
session.delete(s) - Deletig object ‘s’ from database
So finally we need to call commit() in Transaction, like tx.commit();
As i told earlier, when we open session a connection to the database will be created right, so we must close that connection as session. close().
And finally close the SessionFactory as sf.close()
That’s it.., we are done.
Final flow will be______________
Configuration
SessionFactory
Session
Transaction
Close Statements
------------------------------------------------------------------------------------
Hibernate Fav:-
Get Vs Load:-
Based on the above explanations we have following differences between get() vs load():
get() loads the data as soon as it’s called whereas load() returns a proxy object and loads data only when it’s actually required, so load() is better because it support lazy loading.
Since load() throws exception when data is not found, we should use it only when we know data exists.
We should use get() when we want to make sure data exists in the database.
That’s all for hibernate get and load methods, I hope it will clear some doubts and help you in deciding which one to use in different scenarios.
Why use JPA or ORM?
http://binodsuman.blogspot.in/2009/10/jpa-introduction-what-is-jpa-java.htmlWhether the java application will run in the server or without server,
and the application may be desktop or stand alone, swing, awt, servlet…what ever,
but the steps are common to all.In order to work with hibernate we don’t required any
server as mandatory but we need hibernate software (.jar(s) files).
Follow The Steps:
1.Import the hibernate API, they are many more, but these 2 are more than enough…
import org.hibernate.*;
import org.hibernate.cfg.*;
2. Among Configuration, Mapping xml files, first we need to load configuration xml,
because once we load the configuration file, automatically mapping file will be loaded as we registered
this mapping xml in the configuration file. So to load configuration xml, we need to create object of Configuration class, which is given in org.hibernate.cfg.*; and we need to call configure() method in that class, by passing xml configuration file name as parameter.
Eg:
Configuration cf = new Configuration();
cf.configure(“hibernate.cfg.xml”);
Here our configuration file name is your choice, but by default am have been given hibernate.cfg.xml, so once this configuration file is loaded in our java app, then we can say that hibernate environment is started in our program.
So once we write the line_ cf.configure(“hibernate.cfg.xml”), configuration object cf will reads this xml file hibernate.cfg.xml, actually internally cf will uses DOM parsers to read the file.
Finally…
cf will reads data from hibernate.cfg.xml
Stores the data in different variables
And finally all these variables are grouped and create one high level hibernate object we can call as SessionFactory object.
So Configuration class only can create this SessionFactory object
likeSessionFactory sf = cf.buildSessionFactory();
Actually SessionFactory is an interface not a class, and SessionFactoryImpl is the implimented class for SessionFactory, so we are internally creating object of SessionFactoryImpl class and storing in the interface reference, so this SessionFactory object sf contains all the data regarding the configuation file so we can call sf as heavy weight object.
3. Creating an object of session,
Session is an interface and SessionImpl is implemented class, both are given in org.hibernate.*;
When ever session is opened then internally a database connection will be opened, in order to get a session or open a session we need to call openSession() method in SessionFactory, it means SessionFactory produces sessions.
Session session = sf.openSession();
sf = SessfionFactory object
4. Create a logical transaction
While working with insert, update, delete, operations from an hibernate application onto the database then hibernate needs a logical Transaction, if we are selecting an object from the database then we do not require any logical transaction in hibernate. In order to begin a logical transaction in hibernate then we need to call a method beginTransaction() given by Session Interface.
Transaction tx = session.beginTransaction();
session is the object of Session Interface
5. Use the methods given by Session Interface, to move the objects from application to database and from database to application
session .save(s) - Inserting object ‘s’ into database
session.update(s) - Updating object ‘s’ in the database
session.load(s) - Selecting objcet ‘s’ object
session.delete(s) - Deletig object ‘s’ from database
So finally we need to call commit() in Transaction, like tx.commit();
As i told earlier, when we open session a connection to the database will be created right, so we must close that connection as session. close().
And finally close the SessionFactory as sf.close()
That’s it.., we are done.
Final flow will be______________
Configuration
SessionFactory
Session
Transaction
Close Statements
------------------------------------------------------------------------------------
Hibernate Fav:-
Get Vs Load:-
Based on the above explanations we have following differences between get() vs load():
get() loads the data as soon as it’s called whereas load() returns a proxy object and loads data only when it’s actually required, so load() is better because it support lazy loading.
Since load() throws exception when data is not found, we should use it only when we know data exists.
We should use get() when we want to make sure data exists in the database.
That’s all for hibernate get and load methods, I hope it will clear some doubts and help you in deciding which one to use in different scenarios.
No comments:
Post a Comment