Saturday, February 14, 2015

Setting up Hibernate Project in Eclipse

Hibernate Project in Eclipse


          We'll walk through on how to set up the Hibernate Project in Eclipse.

Setting up the Environment

Step1 :  File -> New -> Other -> Java Project


Step2: Provide the Java path Name

Step 3: Now let's add the required Project Libraries. Right click the project -> Properties -> Java Build Path -> Libraries -> Add External Jars  and include the Hibernate related Jar Files ( Note: Hibernate related jar files can be obtained from . Include all the jars within lib/required directory  in the zip) . 

Step 4 : Include the Ojdbc jar as well.

Step 5: Set up the Entry point for the Application by creating the class named TestHibernate with public static void main(String args) and once done, the project structure should be looking as below

Step 6: Let's now create a folder named conf under SampleHibernateProject to place the .cfg.xml and .hbm.xml files. 

Step 7: Add the created folder conf to the ClassPath. Project -> Right Click-> Properties -> Build Path-> Source -> Add Folder -> Select Conf.

Step 8: Let's create the Configuration file .cfg.xml in conf directory. Right click conf directory-> New -> Other -> Hibernate Configuration File -> Provide the Name for .cfg.xml -> Click Next -> Provide the Connection Details as below and Click Next

[Note: since the Create a Console Configuration has been checked in above pic, we are being directed towards creating a Hibernate Configuration Console which helps in validating the connection details provided in the pic above ]

        In the Hibernate Console Config creation , change the Hibernate version to the Latest in the Main Tab and proceed to the optionsTab.

         In the Options Tab, provide the Dialect and proceed to Classpath tab.

[Note: Oracle 10g Dialect is compatible with Oracle 11g DB. We could use Oracle 11g Dialect but it is not officially supported when this post has been created]

          In the Classpath, provide the JDBC Driver as below  and click OK

[Note: Dialect in general means locally Used Language. Dialects in Hibernate helps primarily in converting the HQL to the Native SQL Queries and executing them against the DB]

[Note:  In case if hibernate.cfg.xml becomes unable to be parsed, we would have to replace

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"


<!DOCTYPE hibernate-configuration SYSTEM 

Step 9: We gonna save the data to a table named person having a column named id. Hence let's have the Pojo created named Person with id as one of its property.

Step 10:  Lets's create a Hibernate Mapping File that maps the DB Table person with the above POJO created named Person. Select Conf Folder -> Right Click -> New -> Other -> Hibernate XML Mapping File -> Click Add Class and add the POJO as below -> Click Finish.

Step 11:  Now Let's refer the .hbm.xml created above in our hibernate.cfg.xml created in Step 8. Open hibernate.cfg.xml-> Select Sesison Factory Tab at bottom-> Selection Mapping -> Click Add

                Browse for the Pojo Class and resource .hbm.xml to be mapped. Click Finish.

Step 12: Now place the below snippet in void main and run the Project.

public static void main(String[] args) {
// Instantiate the Configuration to provide the Path
Configuration conf = new Configuration().configure("hibernate.cfg.xml");
//Instanstiate the ServiceRegistry
ServiceRegistry serviceRegistry = new         StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
//Utilize the instance of Configuration and SessionRegistry to create a SessionFactory
SessionFactory sessFact = conf.buildSessionFactory(serviceRegistry);
//Open the Session with sessionFactory
Session session = sessFact.openSession();
//Create an Object to be persisted in DB
Person person = new Person(3456);
//Setup a Transaction 
Transaction transact = session.beginTransaction();
//persisit the object in the Session
//Save the object persisted in the Session

Step 13: The Code gets executed with the Warning Message as of below:

To avod this warning replace <session-factory name=""> as <session-factory> in hibernate.cfg.xml.
The Warning would go Off. We are done. Hurrah !!

No comments:

Post a Comment