Hardik ’s blog

deploying knowledge.jar

Struts 2-Hibernate 3 integration(complete) using eclipse

Posted by hardik4u on September 2, 2008

hi

i am going to give you complete example of struts 2.0.11 and hibernate 3 integration

well let me start from eclipse configuration,i am using eclipse 3.3 for development,it is nice and stable

step1- create Dynamic web project in eclipse .

step2- now i am going to give configuration of struts 2.0.11 in project open web.xml

and add struts filter for applying struts 2 ,write this in web.xml in

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

another thing you have to do is put(create) struts.xml file in src folder of eclipse

and put required library for struts (common-logging.jar,freemarker.jar,ognl.jar,struts2-

core.jar,xwork.jar)

this is basic configuration for struts 2

step-3 now its time to configure hibernate with struts for that we required hibernate and its required

libraries in web-inf/lib folder ,complete list  :

antlr.jar,asm.jar,cglib.jar,commons-beanutils.jar,common-logging.jar,dom4j.jar

freemarker.jar,hibernate.jar,jta.jar, <jdbc drivers libs>, ognl.jar,struts2.jar,xwork.jar

Step4-we have to create session factory of hibernate in struts 2 filter ,so we will extend struts 2 filter,

create class in src folder for create plugin of hibernate

in your own package structure,i have create HibernateUtil.java in org.hns.plugin

HibernateUtil.java

package org.hns.plugin;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;

public class HibernateUtil {

private static SessionFactory sessionFactory;

public static void createSessionFactory() {
sessionFactory = new Configuration().configure().buildSessionFactory();
}

public static Session getSession() {
return sessionFactory.openSession();
}

}



Struts2dispatcher.java

package org.hns.plugin;

import javax.servlet.*;
import org.apache.struts2.dispatcher.FilterDispatcher;
import org.hibernate.HibernateException;

public class Struts2Dispatcher extends FilterDispatcher {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
try {
HibernateUtil.createSessionFactory();
System.out.print(“application initializing successfully”);
} catch (HibernateException e) {
throw new ServletException(e);
}

}

}

Step-5

package org.hns.plugin;

import javax.servlet.*;
import org.apache.struts2.dispatcher.FilterDispatcher;
import org.hibernate.HibernateException;

public class Struts2Dispatcher extends FilterDispatcher {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
try {
HibernateUtil.createSessionFactory();
System.out.print(“application initializing successfully”);
} catch (HibernateException e) {
throw new ServletException(e);
}

}

}

Step-6 – apply this filter in web.xml like

<!–<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> –>
<filter-class>org.hns.plugin.Struts2Dispatcher</filter-class>

Step-7 we have to put hibernate configuration file in src folder with the name hibernate.cfg.xml

<?xml version=’1.0′ encoding=’utf-8′?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>
<session-factory>
<property>true</property>
<property name=”connection.characterEncoding”>UTF-8</property>
<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”connection.url”>jdbc:mysql://localhost:3306/user</property>
<property>usr</property>
<property>usr</property>

<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”current_session_context_class”>thread</property>
<property>

org.hibernate.transaction.JDBCTransactionFactory</property>


</session-factory>
</hibernate-configuration>

give your connection url,username,password,jdbc driver class name,for the example i have used mysql database

i have used user schema with usr  username and password,in user schema there is usermast table with the

field usercode(int),uname(varchar),pwd(varchar),type(varchar), you can create table and put 2-3 dummy entry

in your wishing database.

Step-8

its time to create hibernate mapping with table and mapping class

i have create User.java in org.hns.user package

package org.hns.user.;
public class User
{

private int id;

private String username;

private String password;

private String usertype;

// add fields setter and getter here (required)

}

create User.hbm.xml in org.hns.user (org/hns/user/ OR where User.java created)

like

<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping>
<class table=”usermast”>
<id column=”usercode”>
<generator></generator>
</id>

<property name=”username” column=”uname” />
<property column=”pwd” />
<property column=”type” />

</class>
</hibernate-mapping>

you can get more idea about any another advance mapping stuff from any hibernate tutorial.

here property is java class variable name and column is table column name.

Step-9 add this mapping to hibernate.cfg.xml  using

<mapping resource=”org/hns/user/User.hbm.xml”/>

Step-10 the basic settings for using hibernate for usermast table has been completed here.

Step-11

now i am create another java file UserHibDao.java in  org.hns.user.dao  for create and access user dao

package org.hns.user.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hns.user.User;
import org.sm.plugin.HibernateUtil;

public class UserHibDao {

private List<User> userlist;
private User user;

public void delete(Integer id) {

Session session = HibernateUtil.getSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
user=(User)session.get(User.class,id);
session.delete(user);
tx.commit();

}catch (RuntimeException e) {
if(tx != null) tx.rollback();
throw e;
} finally {
session.close();
}
}

public List getAllUser() {
Session session = HibernateUtil.getSession();
try
{
session.beginTransaction();
userlist=session.createQuery(“from User”).list();
return userlist;

}
catch(Exception e)
{
System.out.print(“Error while fetching “+e);
return null;
}
finally
{
session.close();
}

}

public User getuser(Integer id) {
Session session = HibernateUtil.getSession();
try {
session.beginTransaction();
//change query for get proper data
Query q = session.createQuery(“from User u where u.uid=:id”);
q.setInteger(“userid”,id);
return (User) q.uniqueResult();
}finally {
session.close();
}

}

public void insert(User usr) {
Session session = HibernateUtil.getSession();
Transaction tx=null;
try {
tx = session.beginTransaction();
session.save(usr);
tx.commit();
} catch (RuntimeException e) {
if(tx != null) tx.rollback();
throw e;
} finally {
session.close();
}

}

public void update(User usr) {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx=session.beginTransaction();
session.update(usr);
tx.commit();
}catch (RuntimeException e) {
if(tx != null) tx.rollback();
throw e;
} finally {
session.close();
}

}

}

so after this stuff you can either use this class for creating service layer of dao or you can use this class functions directly for set business logic.

here one thing should be noted that hibernate query fire on its class object not directly on table so i have used

somthing like this session.createQuery(“from User u where u.uid=:id”); instead of using session.createQuery(“from usermast u where u.uid=:id”);[tablename query]

hope this is enough to start with struts 2 and hibernate 3 ,if you feel still there is some stuff missing in this blog ,let me know from comment

Thanks :-) ))

Posted in J2ee, struts-hibernate | 20 Comments »

First Palm Application with SuperWaba

Posted by hardik4u on June 19, 2008

hi,
This is my first Blog To Develop Application for Palm which is Based on Garnet OS
Hope You Will enjoy it.

Let Me Start With SuperWaba Introduction,It is one type of virtual machine(99% JVM) ,Which is use to run java based application on Palm,It has own SDK to develop superwaba application
You can get it from http://www.superwaba.com.br
after download SDK you have to extract this and you find one folder named superwabaSDK
Step 1: Create New Java project in eclipse

Step 2: superwaba VM is compitible with compilation level 1.3 ,So You have to change from

project properties->Compiler->Enable Project Specific settings->Change Compilation Level 1.3

Step 3:You have to build path for using or extending superwaba classes for application
project properties->java build path->libraries->add external jar->from superwabasdk/lib/superwaba.jar

Step 4: extract superwaba.jar(+ icon) select java doc location and give path upto
SuperWabaSDK/docs/html/

Step 5: copy SuperWabaSDK\src\java\superwaba\samples\app\helloworld to your

{Workspace}/{Proejct Name}/{Src}/

(I have done this step bcoz i want to use directly samples into my app,you can create package ,class aslo manually)

Step 6: refresh eclipse project

Step 7: YOu can run and check project using create new run configuration like TestDMS

Main class should be waba.applet.Applet
In argument tab you have to apply
/w 320 /h 320 /scale 1 /bpp16 /useSonyfonts helloworld.HelloWorld
for running helloworld app arguments /w 320 /h 320 /scale 1 /bpp16 /useSonyfonts are optional(this is for sony clie config)
Step 8: now turn comes to create pdb and prc for project
simple way i preffer to you is create jar file of your project from export menu and
put at your c:\suppose HelloWorld.jar(I have suppose your superwabaSDK folder in
C:/)
step 9 : now go to command prompt and at root c:\
step 10: apply this command for pdb
java – classpath /superwabasdk/lib/SuperWaba.jar;/superwabasdk/lib/SuperWabaTools.jar superwaba.tools.Warp c /! /C SSSS HelloWorld HelloWorld.jarafter suceesfully created pdb
step 11 you have to create prc for execution apply this command
java -classpath /superwabasdk/lib/SuperWaba.jar;/superwabasdk/lib/SuperWabaTools.jar superwaba.tools.Exegen /! /C SSSS HelloWorld hello/HelloWorld.class

Posted in SuperWaba Application | Leave a Comment »