Wednesday, December 12, 2018

REST Webservice with Hibernate Framework

In my previous REST Webservice tutorial i explained how to write your first REST Webservice and the other tutorial was about using the Hibernate APIs.

In this example, we shall write the webservice which would perform the database operations using Hibernate APIs.

The example we need a table in database as
 CREATE TABLE TESTPLAYERRESTWS
   ( "ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(50 BYTE),
"SPORTS" VARCHAR2(50 BYTE)
   )

Please download the sample code from here.

For this example we have created 4 methods:

get all player -- > /player
get player by id --> /player/{playerid}
update player -- > /updatePlayer
create player -- > /createPlayer


I have created a tester file along with the application which can be used to test the REST Webservice and its DB operations.


The example is self explanatory. Please feel free to contact in case of any questions

Monday, December 3, 2018

Hibernate For Beginners

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.

Write Your First REST Webservice

I recently learned writing REST based webservice with the help of tutorialspoint.com.
With this blog i intend to write down my learning in an oversimplified form so that it can come handy for anyone who is a naive.

Steps:
1. Create a new Dynamic Web project (in Eclipse goto file -- > New --> Project -- >Web -- > DynamicWeb project)

for this example i intend to deploy it in weblogic which is why selected the mentioned configuration. you may change it as per your requirement.

2. For this example, i intend to create a static list of player and save it in .dat file and i shall be able to access the same through its id or shall be able to access all the players.
Required jar files can be found in attached source code's lib folder
lets jump on coding part:

create 3 java class(detailed code can be found in the attached application, i am putting screenshot to better understand the code):
Player.java : pojo class to contain the player info


PlayerDao.java : to perform the save and retrieve function

PlayerService.java : This class has the methods which will be accessible through REST calls

Here @Path determines the url which would be used to access the webservice
web.xml shall be automatically generated for you, for this example add following entries:

Please note that i have specified the url to start as rest in this case

3. Right click on the project in eclipse and select export --> war File and select a location where the war would be created.

4. deploy this war in weblogic .
The Webservice can be access at :

To get all the players:
http://localhost:<port>/TestRestWSWeblogic/rest/PlayerService/player


to get player by id
http://localhost:<port>/TestRestWSWeblogic/rest/PlayerService/player/2


Here:
TestRestWSWeblogic : is the contect of the application
rest: defined in web.xml in url-pattern tag
PlayerService : @path defined for PlayerService.java class
player: @Path defined for getTrackInJSON method
player/2 :  @Path("/player/{playerid}") defined in getUser method


Sourcecode can be downloaded from here.

Please feel free to post your questions on the same.
In next few blogs, i would be sharing basic hibernate tutorial and its integration with REST Webservice application where it would use more HTTP methods like put/delete