In my last post i did gave an example of how write your first REST Webservice. In this post we will learn to write our first Hibernate Application.
I am developing my application using Hibernate 3. for higher versions, please refer https://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide
Step 1 : We need a table in our database to perform operations. for this example i have created a table named CITY with two fields: Id(Number), name (varchar2)
Step 2: create hibernate.cfg.xml file.
The hibernate.cfg.xml file contains following entry:
<?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-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.url">jdbc:oracle:thin:@<DBHOST>:<DBPORT>/<DBSERVICENAME></property>
<property name="connection.username"><USERNAME></property>
<property name="connection.password"><PASSWORD></property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="city.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Note :
Step 3 : Create the "city.hbm.xml" . This file contains the mapping of CITY table as :
Note : Here the entry :
<class name = "com.loiane.com.model.City" table = "CITY">
points to pojo class named City.java which contains the fields mentioned in CITY table
Step 4: create City.java as:
Step 5 Create CityDao.java to perform DB operations:
necessary jars can be found in the attached source code here. I have written an additional MainClass to test the application. In next tutorial, i shall be explaining how to write a REST Webservice to perform database operations through Hibernate
Please feel free to post your questions on the same.
I am developing my application using Hibernate 3. for higher versions, please refer https://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide
Step 1 : We need a table in our database to perform operations. for this example i have created a table named CITY with two fields: Id(Number), name (varchar2)
Step 2: create hibernate.cfg.xml file.
The hibernate.cfg.xml file contains following entry:
<?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-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.url">jdbc:oracle:thin:@<DBHOST>:<DBPORT>/<DBSERVICENAME></property>
<property name="connection.username"><USERNAME></property>
<property name="connection.password"><PASSWORD></property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="city.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Note :
Step 3 : Create the "city.hbm.xml" . This file contains the mapping of CITY table as :
Note : Here the entry :
<class name = "com.loiane.com.model.City" table = "CITY">
points to pojo class named City.java which contains the fields mentioned in CITY table
Step 4: create City.java as:
Step 5 Create CityDao.java to perform DB operations:
necessary jars can be found in the attached source code here. I have written an additional MainClass to test the application. In next tutorial, i shall be explaining how to write a REST Webservice to perform database operations through Hibernate
Please feel free to post your questions on the same.



No comments:
Post a Comment