Friday, February 15, 2013

Java String Equality Test

public class StringEqualityTest {

    public static void main(String arg[]){
        String str1 = "abc";
        String str2 = "abc";
        String str3= new String("abc");
       
        String str4 = arg[0]; //input "abc" in command-line
        String str5 = arg[1]; //input "abc" in command-line
       
        if(str1 == str2)  // returns true
            System.out.println("Equals 1=2");
       
        if(str1 == str3) // returns false
            System.out.println("Equals 1=3");
       
            if(str4 == str5) // returns false
            System.out.println("Equals 4=5");
    }
}

Thursday, January 12, 2012

Polymorphism by Java Example

public class SuperC {

    public void sayHello(){
        System.out.println("Hello Super");
    }
   
    public void sayBye(){
        System.out.println("Hello Super");
    }
   
    public static void main(String arg[]){
        SuperC sup1 = new SuperC();
        SuperC sub1 = new SubC();
       
        sup1 = sub1; // super class reference pointing to sub-class object
        sup1.sayHello(); // Hello Sub
       
        SuperC sup11 = new SuperC();
        SuperC sub11 = new SubC();
   
        sub11 = sup11; // Super class reference type to super-class object
        sub11.sayHello(); // Hello Super
       
        SuperC sup2 = new SuperC();
        SubC sub2 = new SubC();
       
        sup2 = sub2;  // super class reference pointing to sub-class object
        sup2.sayHello(); //Hello Sub
//       
        SuperC sup3 = new SuperC();
        SubC sub3 =  new SubC();
   
        sub3 = (SubC) sup3; // Exception in thread "main" java.lang.ClassCastException
                            // sub class reference type pointing to super class object
       
        SuperC sup4 = new SubC();
        SubC sub4 = new SubC();
       
        sub4 = (SubC) sup4  ; // Sub class reference pointer pointing to super class reference type
        sub4.sayHello(); // Hello Sub
    }
}

class SubC extends SuperC{

    public void sayHello(){
        System.out.println("Hello Sub");
    }
}

Wednesday, January 11, 2012

Hibernate-Mapping Non Foreign Key in List/Bag

In Hibernate when you want to have a collection representing one-to-many relationship by default Hibernate maps primary key-foreign key mapping for the collection.
In case you want to map an non-foreign key field you could do it by using 'property-ref' attribute of <key> element.

Example:

<class name="com.sample.Item" table="ITEM"
        dynamic-update="false" dynamic-insert="false" select-before-update="false">

        <id name="id" type="int" column="ID">
            <generator class="native">
            </generator>
        </id>
         <property name="itemId"  >
            <column name="ITEM_ID" />
        </property>
        <property name="name"  >
            <column name="NAME" />
        </property>
       <bag name="bidList" lazy="false" order-by="BID_ID" >
            <key column = "BID_ID" property-ref="bidId"></key>
            <one-to-many class="com.example.Bid"/>
      </bag>
</class>

public class Item implements Serializable{
     private static final long serialVersionUID = 8991813750127304998L;

     private int id;
     private int itemId;
     private String name;
     private Collection<Bid> bidList = new ArrayList<Bid>();
  
     public void setId(int id){
           this.id = id;

    }
    public String getId(){
           return id;

    }
    public int getItemId() {
        return itemId;
    }
    public void setItemId(int itemId) {
        this.itemId = itemId;
    }

    public void setName(String name){
           this.name = name;
    }
    public String getName(){
           return name;

    }
    public Collection<Bid> getBidList() {
            return bidList;
    }
    public void setBidList(Collection<Bid> bidList) {
        this.bidList = bidList;
    }
}

public class Bid implements Serializable {

    private static final long serialVersionUID = 7991813750127304558L;
   
    private int id;
    private int bidId;
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getBidId() {
        return bidId;
    }
    public void setBidId(int bidId) {
        this.bidId = bidId;
    }
}