Sunday, 28 July 2013

 Hibernate reverse engineering eclipse                         tutorial:-

Hello friends,
please gone through all steps if you want to learn reverse hibernate engineering.

This tutorail is about how we use hibernate reverse engineering in eclipse

first you have to download JBoss Tools by eclipse market place or either by install ..




Select one option. Here i selected following option because i am using Eclipse Indigo ::: >>
JBoss Tools 3.3 Development Milestone (requires Eclipse)

Now copy page URL and Goto Help > Install New Software.. paste URL in Work with Textbox as shown in below image.



Create Project Structure as shown in below image:-


Here we i create project with the name RevHibernate
And pacakage com.rev.model 


After that then Add Additional jars:-



1.hibernate3.jar
2.javax.persistence.jar
3.mysql-connector-java-5.1.21-bin.jar


Now open hibernate perspective Goto Window > Open Perspective > Other.. as shown in below image.



After that Select Hibernate to open as perspective:-



Then you also find hibernate at right corner of your eclipse




Now Create Database. Here i use mysql to create database.
My Schema name is revhibernate that contains two tables
1. Employee
2. Company
The image of the tables as shown below
1.Employee Table


add foreign key in employee table as shown in below image.



2.Company Table



Now again switch to hibernate then
Press right click on src folder and create new hibernate.cfg.xml file.










Add folowing properties 

1.src path
2.hibernate configuration file name
3. DB type MySQL
4.select driver from drop down
5. url define port and Db connectivity path
6.username and password as per your db
7. check right and then finish.



Press right click on project folder and create new file in project folder hibernate.reveng.xml as shown in below image.






Perform following activity:-

1. select config
2.include only those table thats you want to generate code
3.click on refresh
4.click on finish button




Now select top most small icon and select Hibernate Code Generation Configuration… as shown in below image.





here is check box for require code..
please select as per your requirment.


After run this the result is here :
hbm file and bean class corresponding the db table is create......




Tuesday, 9 July 2013

                           Singleton programs :-

Hello Friends,

I have gone through the concepts of  singleton design pattern  and all possible cases   ,

please check  my programes hope it will be help full to you.                 

The singleton pattern is one of the simplest design patterns: it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.

implement  the concept of Singleton:-


          class Singleton {

 private static Singleton singletonObj;
 private Singleton() {
}
public static  Singleton getSingletonInstance() {
if (singletonObj == null) {
singletonObj = new Singleton();
}
return singletonObj;
}
        /* public static synchronized  Singleton getSingletonInstance() 
         synchronized is used to prevent the simultaneous invocation of the
          method by 2 threads or classes simultaneously.
         */
}

           public class SingletonDemo {

public static void main(String args[]) {
        
Singleton obj1 = Singleton.getSingletonInstance();
                Singleton obj2 = Singleton.getSingletonInstance();
System.out.println(" on invoking method first time singleton instance obtained: " +obj1);
              
                System.out.println("on invoking method second time again same singleton instance obtained: "+obj2);
         
                if(obj1==obj2)
                {
                 System.out.println("singleton concept implemented");
                }
                
}            


         

        This Singleton concept can be breached by two violations viz. Cloneable Interface   

            and Serializable interface:-




c  Class Singleton implements Cloneable {

@Override
      public Object clone() throws CloneNotSupportedException {  
              return super.clone();  
      } 
  private static Singleton singletonObj;
 private Singleton() {
}
 public static  Singleton getSingletonInstance() {
if (singletonObj == null) {
singletonObj = new Singleton();
}
return singletonObj;
  }
        /* public static synchronized  SingletonKapil getSingletonInstance() 
         synchronized is used to prevent the simultaneous invocation of the
          method by 2 threads or classes simultaneously.
         */
}

         public class SingletonViolation{

public static void main(String args[]) throws CloneNotSupportedException {
               
Singleton obj1 = Singleton.getSingletonInstance();
                Singleton obj2 = Singleton.getSingletonInstance();
System.out.println(" on invoking method first time singleton instance obtained:" +obj1);
              
                System.out.println("  on invoking method second time singleton instance obtained:"+obj2);
         
                if(obj1==obj2)
                {
                 System.out.println("singleton concept implemented");
                }
       
                System.out.println(" Violation of  singleton concept using clone()");
                /* Violation of  singleton design pattern using clone method */
               System.out.println("Singleton clone:"+Singleton.getSingletonInstance().clone());
                
}

         To prevent the cloning of singleton object, I have thrown the CloneNotSupportedException and handled it properly:-



             class Singleton implements Cloneable {

private static Singleton singletonObj;
private Singleton() {
}
@Override
         public Object clone() throws CloneNotSupportedException {  
            return super.clone();  
    } 
public static  Singleton getSingletonInstance() {
if (singletonObj == null) {
singletonObj = new Singleton();
}
return singletonObj;
}
        /* public static synchronized  Singleton getSingletonInstance() 
         synchronized is used to prevent the simultaneous invocation of the
          method by 2 threads or classes simultaneously.
         */
        public Object clone1() throws CloneNotSupportedException {  
                 // throw CloneNotSupportedException if someone tries to clone the singleton object  
                 throw new CloneNotSupportedException();  
          }  
}

               public class SingletonPreventCloning  {

public static void main(String args[]) throws CloneNotSupportedException {
               
Singleton obj1 = Singleton.getSingletonInstance();
                Singleton obj2 = Singleton.getSingletonInstance();
System.out.println(" on invoking method first time singleton instance obtained:" +obj1);
              
                System.out.println("  on invoking method second time singleton instance obtained:"+obj2);
         
             
                System.out.println("on cloning exception will be thrown");
                // will throw exception if clone method is called  
               System.out.println("Singleton clone:"+Singleton.getSingletonInstance().clone());
               
}
}