Monday, 9 June 2014

Posted by Unknown  
Tagged as:
14:07

                                      ** Differences Between Save and Persist of session of hibernate ***
                                                                                  In short !                                                                                  



 : save() method returns type is serealizable,and persist() method is void.

: save() returns the id of inserted object in the form of a serealizable object.

:persist() doesnot return the id.

:when Generator class of id is other than assign then we preferred o call save() and In case
  
:In case of assigned generator we call persist().

Thursday, 8 May 2014

Hibernates Advantages and Disadvantages

Posted by Unknown  
Tagged as:
22:00

Hibernates Advantages and Disadvantages



  • Hibernate supports InheritanceAssociationsCollections
  • In hibernate if we save the derived class object,  then its base class object will also be stored into the database, it means hibernate supporting inheritance
  • Hibernate supports relationships like One-To-Many,One-To-One, Many-To-Many-to-Many, Many-To-One
  • This will also supports collections like List,Set,Map
  • In jdbc all exceptions are checked exceptions, so we must write code in try, catch and throws, but in hibernate we only have Un-checked exceptions, so no need to write try, catch, or no need to write throws.  Actually in hibernate we have the translator which converts checked to Un-checked ;)
  • Hibernate has capability to generate primary keys automatically while we are storing the records into database
  • Hibernate has its own query language, i.e hibernate query language which is database independent
  • So if we change the database, then also our application will works as HQL is database independent
  • HQL contains database independent commands
  • While we are inserting any record, if we don’t have any particular table in the database, JDBC will rises an error like “View not exist”, and throws exception, but in case of hibernate, if it not found any table in the database this will create the table for us ;)
  • Hibernate supports caching mechanism by this, the number of round trips between an application and the database will be reduced, by using this caching technique an application performance will be increased automatically.
  • Hibernate supports annotations, apart from XML
  • Hibernate provided Dialect classes, so we no need to write sql queries in hibernate, instead we use the methods provided by that API.
  • Getting pagination in hibernate is quite simple.

Disadvantages of hibernates:

  • I don’t think there are disadvantages in hibernate
  • You know some thing.., Its saying hibernate is little slower than pure JDBC, actually the reason being hibernate used to generate many SQL statements in run time, but i guess this is not the disadvantage :-)
  • But there is one major disadvantage, which was boilerplate code issue, actually we need to writesame code in several files in the same application, but spring eliminated this

Monday, 5 May 2014

hibernate

Posted by Unknown  
Tagged as:
01:11

Hibernate:

Introduction

Posted by Unknown  
Tagged as:
00:00

                         Hash Code and Equals method in Java object 



Java.lang.Object has methods called hasCode() and equals(). These methods play a significant role in the real time application. However its use is not always common to all applications. In some case these methods are overridden to perform certain purpose. In this article I will explain you some concept of these methods and why it becomes necessary to override these methods.

hashCode()

As you know this method provides the has code of an object. Basically the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value.

equals()

This particular method is used to make equal comparison between two objects. There are two types of comparisons in Java. One is using “= =” operator and another is “.equals()”    “equals()”.refers to equivalence relations. So in broad sense you say that two objects are equivalent they satisfy the “equals()” condition.

public boolean equals(Object obj) 
{
 return (this == obj);
}

Now I will explain you when to override the equals() and hashCode() methods and why it is necessary to override these methods. In this regard there is a rule of thumb that if you are going to override the one of the methods( i;e equals() and hashCode() ) , you have to override the both otherwise it is a violation of contract made for equals() and hashCode(). 


case1 You can override the hashCode method in your own way. Please refer to the following example.

public class NewClass12 
{


private int age ;

public NewClass12 ( int age )
{
super();
this.age = age;
}

public int hashCode()
{
return age;
}

}
In the above example class “NewClass12” the variable age is the significant factor. Here the hashCode value will return the age of the person. Now let us consider the following test class.


public class NewClass 
{
    public static void main(String args[])
    {
        
        
        NewClass12 nc = new NewClass12(23);
        System.out.println("hash code:" + nc.hashCode());
       int originalHashCode = System.identityHashCode(nc);
      System.out.println("Original hashCode of NewClass12---->>>"+originalHashCode);
        
    }
    

}


Here the output will be like this Overridden hashCode()--->>>23 Original hashCode of NewClass12--->>>19770577 As you know the above number is arbitrary, it depends upon your system. So then why it is necessary to override this method. There is one reason that if want to compare two objects based upon the equals() method. Although in a very simple class like “NewClass12”, you can achieve without overriding hashCode() method. But if you do this , you are going to violate the contract for the methods hashCode() and hashCode() of the object class. The similar case is for the method equals(). So funcational point is that if want to compare two objects based upon the equals() method you have to override both hashCode() and equals() methods.


public class NewClass 
{
    public static void main(String args[])
    {
        
        
        NewClass12 nc = new NewClass12(23);
         NewClass12 nc1 = new NewClass12(23);
         System.out.println("nc.equals(nc1)--->>>"+nc.equals(nc1));

        System.out.println("hash code:" + nc.hashCode());
       int originalHashCode = System.identityHashCode(nc);
      System.out.println("Original hashCode of NewClass12---->>>"+originalHashCode);
        
    }
    

}
case 2: Think of a test scenario where you want to store your objects in a HasSet and you want to find a particular object. First let us see if we do not override the methods and we want to store the objects in the HashSet. Let us analyse the impact of it from the following code.

public class NewClass12 
{


private int age ;

public NewClass12 ( int age )
{
super();
this.age = age;
}

public int hashCode()
{
return age;

}

In the above code it is a normal class. Now let us see the test harness class.

public class TestClass 


  
{
public static void main(String[] args) 
{
NewClass12 nc1 = new NewClass12(23);
NewClass12 nc2 = new NewClass12(24);
NewClass12 nc3 = new NewClass12(25);
NewClass12 nc4 = new NewClass12(26);
NewClass12 nc5 = new NewClass12(27);
HashSet<NewClass12> hs = new HashSet<NewClass12>();
hs.add(nc1);
hs.add(nc2);
hs.add(nc3);
hs.add(nc4);
hs.add(nc5);

System.out.println("HashSet Size--->>>"+hs.size());
System.out.println("hs.contains( new NewClass12(25))--->>>"+hs.contains(new NewClass12(25)));
System.out.println("hs.remove( new NewClass12(24)--->>>"+hs.remove( new NewClass12(24)));
System.out.println("Now HashSet Size--->>>"+hs.size());
}
}  


If you run the above program, the will output will be like the following. 
output

HashSet Size--->>>5
hs.contains( new NewClass12(25))--->>>false
hs.remove( new NewClass12(24)--->>>false
Now HashSet Size--->>>5


It means that you can not find the object. However it is not the case for Integer object. You can put object of type Integer in a HashSet and you can try and you can see the effect. Now let us modify the “NewClass12” class so that we will get over the problems what we faced in the above test harness class.

public class NewClass12 
{


private int age ;

public NewClass12 ( int age )
{
super();
this.age = age;
}

public int hashCode()
{
return age;
}

    

public boolean equals( Object obj )
{
boolean flag = false;
NewClass12 nc = (NewClass12 )obj;
if( nc.age == age )
flag = true;
return flag;
}

}
Here in the above class, we have overridden the hashCode() and equals() methods. Now if you run the same test harness class, you will get the desired output like the following. 
HashSet Size--->>>5
hs.contains( new NewClass12(25))--->>>true
hs.remove( new NewClass12(24)--->>>true

Now HashSet Size--->>>4

case 3: In this case you want to use your object as key not the value in the HashMap. So you have to override both the methods hashCode() and equals(). However it is left to the reader to create the object and test the feature in a Map. 

case 4: If want to make your own immutable object , it will be wiser to override the equals() and hashCode() methods. To test the above programs, please create the appropriate package as mentioned in the program. You can also create your own package and modify the package name in the above programs. You can all the code in your favorable java editor.





Wednesday, 23 April 2014

Posted by Unknown  
Tagged as:
21:54

when you're talking about "new mutable field and override getter of an existing previously immutable field" you're talking about new class. Class immutability is not inherited property. It is quite possible to create mutable class by extending immutable one. Also let me point out that getter has no special connection to corresponding field from JVM point of view - it is just programming paradigm, design pattern if you will. So when you override getter you don't do anything to final field of the immutable class - let me show you: 
public class Immutable1 { 
public final int a; 
public Immutable1(int a){ 
this.a = a; 


public int getA(){ 
return a; 



class Mutable extends Immutable1 { 
int a; 
public Mutable(int a){ 
super(a); 
this.a = a; 

public int getImmutableA(){ 
return super.getA(); 


public int getA(){ 
return a; 


public static void main(String[] args){ 
Mutable aa= new Mutable(5); 
aa.a= 6; 

System.out.println("Mutable "+aa.getA()); 
System.out.println("Immutable"+aa.getImmutableA()); 


Pages

What they says

Proudly Powered by Ujwala Rathod.
back to top