Saturday 27 July 2013

first hibernate application



























steps:

1. create the java project:

go to file -> new->project->"give any name" i.e: HibernateAppli. click on next->finish button

2. Add jar files for hibernate
//download the hibernatjar files

to add the jar files: right click on your project->build path->Add external archives.

now select all the jar files as show in the image given below then click open.
3. in this example we are connecting the application with MySql database. So you must add the
(i have already installed MYsql database)
//download mysql jar files

go to project->right click->properties->click on java build path->click on Libraries->Add External JARs
click on ok

//creating first hibernate program

//creating package
 1. go to file->new->package->

Using the New Java Package wizard

Once the Java Package wizard comes up:

    Enter/confirm the source folder name

    Enter the package name

    Click on the Finish button


4. create the persistance class
create employee class into com.infy

now create a package as com.infy and create Employee class into com.infy package
->place the curson on newly created package(i.e infy.com)
->right click->choose class
new java class wizard shows
->give a class name. i.e Employee
->click on finish
5. create the mapping file for persistent class

->here we are creating the same mapping file, Right click on src->new->file->specify the file name(e.g:employee.hbm.xml)->ok
->it must be outside the package:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="com.infy.Employee" table="InfyEmployee">
 <id name="empNo">
 <generator class="assigned"></generator>
 </id>

 <property name="empName" length="20"></property>
 <property name="doj" type="Date" length="20"> </property>
 <property name="dob" type="Date" length="20"> </property>
 <property name="phno" type="String" length="12"></property>

 </class>
 </property-mapping>

->here persistance entity class is "Employee"

table is Database is "InfyEmployee"
here id property is empNo, here identifier generator


(5) create the Configuration file

configuration file contains all the informations for the database such as connection_url, driver_class, username,password etc
The hbm2ddl.auto property is used to create to create the table in the database automatically.
we will have in-depth learning about Dialect class in next topics. To create the configuration file, right click on src->new->file.
 specify the configuration file name e.g. hibernate.cfg.xml.

//hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-confuguration>
<session-factory>
<!-- Database connection settings -->


 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="connection.url">jdbc:mysql:://localhost:3306/poc</property>
 <property name="connection.username">user</property>
 <property name="connection.password"/>

 <!--JDBC connection pool (use the built-in)-->
 <property name="connection.pool_size">1</property>


 <!--SQL dialect-->
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

 <!--Disable the second -level ache-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

 <!--Echo all executed SQL TO stdout-->
 <property name="show_sql">true</property

 <!--drop the existing tables and create new one-->
 <property name="hbm2ddl.auto">create</property>

 <!--mention here all the model classes along with their package name-->
 <mapping class="infy.com.Employee"/>

<session-factory>
<hibernate-configuration>


(6) create the class that retrieves or stores the persistent object
es
->in this class, we are simply storing the employee object to the database.













































No comments:

Post a Comment