Thursday, August 12, 2010

Hibernate JPA 2.0 on glassfish v3 with Netbeans IDE setup tutorial

After it took me almost a day to get Hibernate working properly on GF I thought I'd write here how it's done.

In this tutorial I used the following products:
  • NetBeans 6.9.1 - get the JEE version at least
  • Glassfish 3.0.1
  • Mysql
and libraries:
  • Hibernate 3.5.3
  • Slf4j-1.5.8 (you need to get the same version as the API module distributed with hibernate - in this case  slf4j-api-1.5.8.jar)
  • Log4j 1.2.16
  • mysql-connector-java-5.1.11-bin.jar - this is used in GF to connect to MySQL databases.
Make sure you get all of the above before proceding.
From now on I'll assume you have NetBeans, Glassfish & MySql installed and working properly.

Creating a database
Use the following sql dump to create a test database:

/*
SQLyog Ultimate v8.3 
MySQL - 5.0.77 : Database - tutorial
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`tutorial` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `tutorial`;

/*Table structure for table `employee` */

DROP TABLE IF EXISTS `employee`;

CREATE TABLE `employee` (
  `employeeId` int(11) NOT NULL auto_increment,
  `employeeName` varchar(100) default NULL,
  PRIMARY KEY  (`employeeId`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

/*Data for the table `employee` */

insert  into `employee`(`employeeId`,`employeeName`) values (1,'John Doe'),(2,'Frank Bates'),(3,'Alex Smith');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


Also, don't forget to create a user with full access to this DB.

Friday, December 11, 2009

Integrating Google Web Toolkit (GWT 2.0) with CodeIgniter

Integrating GWT with a php framework can be a little tricky since there isn't much documentation on the web on the subject.

First of all you will need two projects:
- one for the CodeIgniter server side PHP application
- one for the Java GWT client application


These are the main steps needed to setup GWT with CI.

1) Setup CodeIgniter configuration:
- change in "config.php" the following setttings:

$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = TRUE;


These are required only in development mode to allow GET variables in url . For production use you can revert to your original settings.

2) Setup the host view
Normally GWT starts up from a "host" html file, but in CI this needs to be done from inside a view.
Let's assume that "gwthost.php" is the view file where you want to display the GWT Application:

<!-- the rest of your HTML here -->

<script type="text/javascript" language="javascript"
src="GWT_MODULE/GWT_MODULE.nocache.js"></script>

<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1'
style="position:absolute;width:0;height:0;border:0"></iframe>

<!-- we define a place where the GWT App will show up -->
<div id = "gwt_container"> </div>


GWT_MODULE is the name of the module you'll create for your GWT Project (step 3)

You will load the view as usual from a controller function:
/* controller test.php */

function showGWT(){
$this->load->view('gwthost');
}


3) In this step we will configure the GWT Application to run without the built in server.

I assume you successfully installed GWT SDK 2.0 and Eclipse plugin and that you are able to compile and run the included samples. Please refer to the official google docs for reference.


  • Open or create a GWT project in Eclipse

  • first setup the compile path:

  • Click the "GWT Compile Project" button, then in the Advanced section, Additional compiler arguments enter:

    -war PATH_TO_SERVER_APP

    PATH_TO_SERVER_APP is the file system path for server application (web server folder), for example:

    -war d:\xampp\htdocs\gwttest

  • Click Compile

  • At this point, if the web server is started you should be able to access your application from a browser (http://localhost/gwttest/test/showgwt )

    remember the test.php controller ;)

    At this point we configured gwt to run in "web mode". You need to recompile the project after each modification.

    To enable "dev mode" follow the next step:
  • Open the "Run Configurations" for your project

  • In Main tab, disable "Run built-in server"

  • Go to "Arguments" tab and enter the following parameter:

  • -startupUrl http://CI_PAGE_URL

    ex:

    -startupUrl http://localhost/gwttest/test/showgwt

    If you run this configuration it will prompt you to use an url like this one:
    http://localhost/gwttest/test/showgwt?gwt.codesvr=10.0.0.1:9997


Paste this in your browser and it will prompt you to install the GWT Plugin and you should be running in "dev mode".
This means that if you change the source code you don't have to recompile the GWT application; just refresh the browser.