MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Using Google Web Toolkit with Hibernate

In this article we will see how to work with Google Web Toolkit(GWT) together with Hibernate in Java development

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

The Google Web Toolkit (GWT) is a development toolkit used in order to develop and optimize the complex web based applications. GWT is the web development framework that helps the developers to create the high quality applications in java. Hibernate and GWT can be used together in order to get the related services and get maximum benefits by using them in an application.

The hibernate provides a solution of Object Relational Mapping (ORM) in java and this framework can be used in order to make the database tables from classes of java applications in a direct manner respectively. The framework of GWT is based on java and therefore the java based Integrated Development Environment (IDE) can be used to develop the browser based applications of GWT.

In this article, the related importance and description of both powerful frameworks has been elaborated and the work of integration of both these frameworks is done in an easy manner. The latest technologies have helped the developers and provided excellent methods and new ways of doing work on the applications by writing minimum code.

The data types available in the classes of the java application have the ability to get converted to the data types of SQL in an easy manner by using the techniques present in this object-relational mapping library. The ORM library uses the java database connectivity (JDBC) API in order to interact and get the access to the database system that is required to be used in the particular application for the storage purpose respectively. There is an availability of a query language in Hibernate which is known as Hibernate Query Language (HQL) and with the help and usage of this language the queries written in SQL are allowed in order to access the data objects present in hibernate.

HQL helps in generation of the database independent queries and it is basically a different version of SQL that is known as the object-oriented. HQL basically helps in the generation of database independent queries and that is a different version of SQL and is known as the object-oriented version. There was a requirement to change the queries if the database attached is required to be changed but now with the usage of this powerful framework of hibernate the problems have finished and the developer does not need to worry about the manual work with the database system.

The development time of the developers has successfully reduced when programming the web application by the use of hibernate library. GWT provides an event handler model that is similar to the user interface frameworks of java that includes Abstract Window Toolkit (AWT) and SWING. The mechanism that is basically used by GWT is known as the Remote Procedure Call (RPC) and with the use of this mechanism the client side gets the ability to execute the methods of server side in a direct manner.

The GWT RPC is based on servlet and the server side servlet is known as service because the client basically gets the related required services from the server when there is a need to access the respective methods and calls.

Overview:

The work is done in the following manner, first the projects are created for the required application and related setup is done at the start, then the user interface related work is planned to be done, then the integration related work of GWT application in order to access the available and required database system through the hibernate framework is done after the proper developed database system when that has been made accessible respectively. An application which is based on GWT generally consists of the modules that include the client side and the server side respectively.

The code of client side runs in the web browser and the code of server side runs in the web server. The client side needs to make the HTTP request in order to access the data of server side across the network and gets the response in the respective manner. In order to make the application running with persistence for the objects of hibernate there is a need to use the HSQLDB that is the Hibernate SQL query language for the database system.

The mechanism that is used by GWT is known as the Remote Procedure Call (RPC) and by the use of this respective mechanism the client side has the ability to execute the methods of server side directly. There is need to create the client side interfaces for the service purpose. When the application work of hibernate and GWT has started to work successfully then the server side GWT RPC services get ready to perform the related database system operations in a successful manner.

Importance:

The development time gets less very much when the programming of an application is done using this library. The framework of GWT is used at Google by different products. There are many different components available in GWT framework and it can be divided into different parts including GWT java to java-script compiler and GWT user interface building library.

The data driven web based applications can be developed by the use of data abstraction technology known as hibernate and integrating it with the Google Web Toolkit (GWT) respectively. The browser based applications of GWT framework can be executed and have the ability to run on different modes that can be either the development mode or the production mode. The Google Web Toolkit (GWT) applications have the ability to run on respective web browsers as well as on the smart phones which are Android and IOS based phones and the tablets respectively.

The code of JavaScript can be automatically generated by GWT framework which is suitable for the respective browser. The browser or web based applications that are created by the developers by the use of GWT can be maintained easily.

Screenshots:

Using Hibernate, the data has been inserted inside the table and GWT is used for the overview of webpage work to show in the browser using the theme of chrome. Following are the screens from the running and working application:

Hibernate configuration file, output and related files inside the Projects tab

Figure 1: Hibernate configuration file, output and related files inside the Projects tab

The Figure 1 shows the work of an application in which the left side of Projects tab shows the total files inside the packages and the related libraries of both GWT as well as of Hibernate and are included to do the related work.

Row entered inside the table with the use of Hibernate

Figure 2: Row entered inside the table with the use of Hibernate

The Figure 2 shows the output of table and the rows were inserted using hibernate without writing any SQL. The code files are also added and the related file Main.java is run by the click and selecting to run file and accordingly after the successful execution the data gets inserted inside the table of database.

GWT Web Application Code Files and Package including the libraries of Hibernate and GWT

Figure 3: GWT Web Application Code Files and Package including the libraries of Hibernate and GWT

The Figure 3 shows the output of successful build of web application. The left side shows the related files and packages used to do the work of GWT application and the main screen shows the related code of the main file.

Output of Web Application

Figure 4: Output of Web Application

The Figure 4 shows the work of GWT when run after the successful build in the web browser, the about button when clicked shows the name and details of web application, when the view entered message button is clicked the inserted message by the user inside the text-field is shown after the button respectively.

The output of web application after the changes in configuration and classes

Figure 5: The output of web application after the changes in configuration and classes

Implementation & Codes:

The code work is shown of both hibernate and GWT from the running application and the files can be verified from the screens of Figure 1 and Figure 3 respectively.

The code work of hibernate:

Listing 1: 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-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/Management</property>
    <property name="hibernate.connection.username">use</property>
    <property name="hibernate.connection.password">system</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <property name="hibernate.connection.autocommit">true</property>
    <property name="hibernate.hbm2ddl.auto">validate</property>
    <mapping resource="org/hiber/work/Employee.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

This contains all the related configurations used to run the work of hibernate in this application in a successful manner.

Listing 2: hibernate.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-schema="USE"/>
  <table-filter match-name="EMPLOYEE"/>
</hibernate-reverse-engineering>

This file is a kind of configuration and the reverse engineering file of hibernate in which the tags work matches the user name and the table.

Package Name: org.hiber.work

Listing 3: Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="org.hiber.work.Employee" table="EMPLOYEE" schema="USE">
        <id name="EId" type="int">
            <column name="E_ID" />
            <generator class="assigned" />
        </id>
        <property name="EName" type="string">
            <column name="E_NAME" length="60" not-null="true" />
        </property>
        <property name="EAddress" type="string">
            <column name="E_ADDRESS" length="300" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

This file shows the mapping of table to the file and it is also a kind of configuration file and it finds the information from the database table when the connection has been successfully established and the tables selected is available inside the database system.

Listing 4: Employee.java

package org.hiber.work;
/**
 * Employee generated by hbm2java
 */
public class Employee  implements java.io.Serializable {
     private int EId;
     private String EName;
     private String EAddress;

    public Employee() {
    }
    public Employee(int EId, String EName, String EAddress) {
       this.EId = EId;
       this.EName = EName;
       this.EAddress = EAddress;
    }
      public int getEId() {
        return this.EId;
    }
      public void setEId(int EId) {
        this.EId = EId;
    }
    public String getEName() {
        return this.EName;
    }
      public void setEName(String EName) {
        this.EName = EName;
    }
    public String getEAddress() {
        return this.EAddress;
    }    
    public void setEAddress(String EAddress) {
        this.EAddress = EAddress;
    }
}

This is the class in which the attributes are available and these are mapped to the configuration files as well. There are getters and setters which set and get the values respectively when this class parameterized constructor is called.

Listing 5: Main.java

package org.hiber.work;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main {
    public static SessionFactory fac;
    public static void main(String [] args){
         try{
             fac=new Configuration().configure().buildSessionFactory();                        
        }catch(Exception e){
            e.getMessage();            
        }
         Session sess=fac.openSession();
         Transaction ts=null;
         try{
             ts=sess.beginTransaction();
//           Employee em=new Employee(12,"ABCD EFGH", "sdkmnk sjfdn skdm");
//           Employee em=new Employee(15,"EF GH","asndjsjfdn skdm ffrve");
             Employee em=new Employee(17,"Hji Jui","house # 1, street # 5,sajid");
             sess.save(em);
             ts.commit();
             sess.close();
         }catch(Exception e){
             e.getMessage();
         }
    }
}

This is the main file and it is run by specifically selecting this file because it contains the main method and the related work of insertion is done through this file.

The code work of GWT:

Listing 6: welcomeGWT.html

<html>
    <head>
        <meta name='gwt:module' content='org.yournamehere.Main=org.yournamehere.Main'>
        <title>Main</title>
        <script type="text/javascript"  src="org.yournamehere.Main/org.yournamehere.Main.nocache.js"></script>
    </head>
    <body>
        <iframe src="javascript:''" id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
    </body>
</html>

This is the welcome file of GWT application in which the related tags are available.

Listing 7: webxml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>GWTService</servlet-name>
        <servlet-class>org.yournamehere.server.GWTServiceImpl</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GWTService</servlet-name>
        <url-pattern>/org.yournamehere.Main/gwtservice</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>welcomeGWT.html</welcome-file>
    </welcome-file-list>
</web-app>

This is the configuration file for GWT related files and the calls to files are made from this file when application is executed.

Package Name: org.yournamewhere

Listing 8: Main.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
<module>
    <inherits name="com.google.gwt.user.User"/>
    <inherits name="com.google.gwt.user.theme.dark.Dark"/>
    <entry-point class="org.yournamehere.client.MainEntryPoint"/>
</module>

This is the main configuration file in which the theme and calls to the desired classes are made.

Package Name: org.yournamewhere.client

Listing 9: GWTService.java

package org.yournamehere.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("gwtservice")
public interface GWTService extends RemoteService {
    public String myMethod(String s);
}

This is an interface in which a method has been defined.

Listing 10: GWTServiceAsync.java

package org.yournamehere.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface GWTServiceAsync {
    public void myMethod(String s, AsyncCallback<String> callback);
}

This is an interface in which a method has two parameters.

Listing 11: GWTServiceUsageExample.java

package org.yournamehere.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
public class GWTServiceUsageExample extends VerticalPanel {
    private Label lblServerReply = new Label();
    private TextBox txtUserInput = new TextBox();
    private Button btnSend = new Button("View Entered Message"); 
    public GWTServiceUsageExample() {
        add(new Label("Enter text: "));
        add(txtUserInput);
        add(btnSend);
        add(lblServerReply);
        // Create an asynchronous callback to handle the result.
        final AsyncCallback<String> callback = new AsyncCallback<String>() {
            public void onSuccess(String result) {
                lblServerReply.setText(result);
            }
            public void onFailure(Throwable caught) {
                lblServerReply.setText("Communication failed");
            }
        };
        btnSend.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                // Make remote call. Control flow will continue immediately and later
                // 'callback' will be invoked when the RPC completes.
                getService().myMethod(txtUserInput.getText(), callback);
            }
        });
    }
    public static GWTServiceAsync getService() {
        return GWT.create(GWTService.class);
    }
}

This is the class which contains the declarations of different attributes and the related methods and calls to the related methods of other classes respectively.

Listing 12: MainEntryPoint.java

package org.yournamehere.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
public class MainEntryPoint implements EntryPoint {
    public MainEntryPoint() {
    }
    public void onModuleLoad() {
        final Label label = new Label("GWT Work in Web Application and Hibernate also used!!!");
        final Button button = new Button("About!");        
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                label.setVisible(!label.isVisible());
                Window.alert("THIS IS FINAL WORK DONE");//This message shown as a message box in the browser when the button is clicked
            }
        });        
        RootPanel.get().add(button);
        RootPanel.get().add(label);
        RootPanel.get().add(new GWTServiceUsageExample());
    }
}

This is the class which is shown at the output and is called by the configuration files in a proper manner.

Package Name: org.yournamewhere.server

Listing 13: GWTServiceImpl.java

package org.yournamehere.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.yournamehere.client.GWTService;
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
    public String myMethod(String s) {
        return "This is the entered message: " + s;
    }
}

This is the method which is called after the user enters the text in the text-field and this displays it respectively. The output screen is shown of this work as well.

This is all for this article. Hope you liked it.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login