Wednesday, June 29, 2011

Put delay in JavaScript

Try use this function
---------------------

function pauseJS(ms) {
var date = new Date();
var curDate = null;
do {
curDate = new Date();
}while(curDate-date < ms);
}

this function stops the browser so nothing will work during that time
while setTimeout() didnt.

Tuesday, June 28, 2011

JavaScript trim() function not working in FF or IE

Copy the code given below in your java script tag and get the trim() function working

if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}

hibernet.cfg.xml

Beans.xml



Hibernet Basics to Work With Database

-------------------------
Beans .java file
-------------------------
package my.beans;
/**
*
* @author raj
*/
public class Pen {
private int id;
private String penName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPenName() {
return penName;
}
public void setPenName(String penName) {
this.penName = penName;
}
}
---------------------------------------------------------------------
Methods To Store and Display Data of Pens
---------------------------------------------------------------------
//default package
public class MyHIbernateBasics{
public static void main(String rk[]){
MyHIbernateBasics start=new MyHIbernateBasics();
start.storePenData("PinkPen");
start.showPensData();
start.updatePensData();
}
public void storePenData(String penName) {
Session mySession = HibernateUtil.getSessionFactory().getCurrentSession();
mySession.beginTransaction();
Pen pen= new Pen();
pen.setPenName(penName);
mySession.save(pen);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
public void showPensData(){
Session mySession = HibernateUtil.getSessionFactory().getCurrentSession();
mySession.beginTransaction();
System.out.println(".............PENS DATA..........");
List pensList = session.createQuery("from Pens").list();
for (int i = 0; i < pensList.size(); i++) {
Pen myPen = pensList .get(i);
System.out.println("Id : " + myPen.getId() + " Name : " + myPen.getPenName() );
}
mySession.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
public void updatePensData(){
//UPDATIND DATA INTO Pens TABLE
Session mySession = HibernateUtil.getSessionFactory().getCurrentSession();
mySession.beginTransaction();
System.out.println("..............Pens Data..................");
Vector allPens= new Vector(session.createQuery("from Pens").list());
for (Pen p : allPens) {
if (p.getPenName().equals("PinkPen")) {
p.setPenName("RK");
mySession.update(p);
}
}
//DISPLAYING DATA OF THE Pens TABLE
for (Pen myPen : allPens) {
System.out.println("Id : " + myPen.getId() + " Name : " + myPen.getPenName() );
}
mySession.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
----------------------------------------------------------
Java Class file to Get SessionFactory
----------------------------------------------------------
package my.utils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
public static SessionFactory sessionFactory=null;
static
{
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
}
catch(Throwable e){
System.out.println(" :-( Error Message: "+e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

Sunday, June 26, 2011

BASICS OF HIBERNET

HIbernate Basics Help Documents
-------------------------------------------------
Features :
----------------
Open Source
ORM
Lazy loadiing
First level caching
Automatic Dirty checking
Second Level caching
Advantages

ORM : Object Relational Mapping
: Mapping object with particular table Uing the .hmb (Hibernate Mapping File) file. To load of objects into the particular rows and columns of the table.
: Also retrieve data from the table and load the data into the object instance or properties or objects member variables.

Lazy loading :

Fetching data from the database to the application. Unless you use that object in application its just a proxy. so when u will certainly going to use the object then it will retrieve the data.

Advantage :
better performance, does not execute unnecessary queries, lock time on the table is minimal, data accessing time is high.

First level cache:
If we want to retrieve an object representing a particular record of database having a primary key of 109 then first
it will look the framework whether is there any object with the same primary key ! if so then hibernate will give you
the reference of that particular object. so it will save the execution time and give better performance. it will not
execute unnecessary query to access the data from the database

Automatic dirty checking :

simply modify the collection using the normal collection methods. There is no explicit call to update() or save(); Hibernate automatically detects that the
collection has been modified and needs to be updated. This is called automatic dirty checking.

Second level caching :
Uses Physical memory to cache the particular object/table so faster retrieval of data being possible.
Flushing : The process of synchronizing the memory state with the database, usually only at the end of a unit of work, is called flushing.

Advantages :

Database in-dependency
Switching between database is easy
minimal time taking to switch the database
Need to change the Configuration file.
No specific syntax to follow.
No need of application server like weblogic, JBoss, EJB, Websphere etc.
Simple tomcat server is sufficient and efficient
Flexibility of collection pooling