| Arun.S.L 2004-10-14, 3:58 pm |
| Hi,
I have a database table with composite primary key, consisting of 5
fields.
2 of the fields are null for my application. In database those two
columns are unique. I need to implement this in my primary key class.
I'm unable to access the record using the findByPrimaryKey. If I pass
null or zero for the two values, its not accepting. How do i implement
it in my hashcode and equals methods.
My query is as follows...........
select user_id, created_date, modified_date, created_by,
modified_by, is_primary, contact_type from internal_contacts where
subscriber_id = ? and user_id = ? and cust_system_code = ? and
tran_id is null and invoice_no is null ";
Here's my primary key class.........
public final class InternalPK extends java.io.Serializable
{
public long subscriberId = -1;
public String userId = null;
public String custSystemCode = null;
public String invoiceNo = null;
public String tranId = null;
private int mHashCode = -1;
public InternalPK (){}
public InternalPK (long aSubscriberId, String aUserId, String
aCustSystemCode, String aInvoiceNo, String aTranId)
{
this.subscriberId = aSubscriberId;
this.userId = aUserId;
this.custSystemCode = aCustSystemCode;
this.invoiceNo = aInvoiceNo;
}
public long getSubscriberId ()
{
return subscriberId;
}
public void setSubscriberId (long aSubscriberId)
{
this.subscriberId = aSubscriberId;
}
public String getUserId ()
{
return userId;
}
public void setUserId (String aUserId)
{
this.userId = aUserId;
}
public String getCustSystemCode ()
{
return custSystemCode;
}
public void setCustSystemCode (String aCustSystemCode)
{
this.custSystemCode = aCustSystemCode;
}
public String getInvoiceNo ()
{
return invoiceNo;
}
public void setInvoiceNo (String aInvoiceNo)
{
this.invoiceNo = aInvoiceNo;
}
public String getTranId ()
{
return tranId;
}
public void setTranId (String aTrandId)
{
this.tranId = aTranId;
}
/**
* Returns the hash code for this primary key.
*/
public int hashCode ()
{
//Compute the hashcode only once
if (mHashCode == -1)
{
mHashCode = ((int)(subscriberId
^ userId.hashCode ()
^ custSystemCode.hashCode ()
^ (invoiceNo==null?0:invoiceNo.hashCode())
^ (tranId==null?0:trandId.hashCode())));
}
return mHashCode;
}
/**
* Checks if the speified object is same as this object.
*
* @param aObj Object to be checked for equality
* @returns true if the object is equal to this object; false
otherwise.
*/
public boolean equals (Object aObj)
{
//Return immediately if the same object.
if (aObj == this)
{
return true;
}
//Check for equality
boolean isEqual = false;
//TODO Make sure that the if condition is correct...
if (aObj instanceof InternalPK)
{
if ((aObj.hashCode () == hashCode ()) &&
(((InternalPK) aObj).subscriberId == (subscriberId)) &&
(((InternalPK) aObj).userId.equals(userId)) &&
(((InternalPK) aObj).custSystemCode.equals(custSystemCode)) &&
(((InternalPK) aObj).invoiceNo.equals(invoiceNo)) &&
(((InternalPK) aObj).trandId.equals(tranId)))
{
isEqual = true;
}
else
{
isEqual = false;
}
}
else
{
isEqual = false;
}
return isEqual;
}
/**
* Return the string representation of this object.
*/
public String toString ()
{
String lineSeparator = System.getProperty ("line.separator");
StringBuffer buffer = new StringBuffer ();
buffer.append ("subscriberId = ").append (subscriberId).append
(lineSeparator);
buffer.append ("userId = ").append (userId).append
(lineSeparator);
buffer.append ("custSystemCode = ").append (custSystemCode).append
(lineSeparator);
buffer.append ("invoiceNo = ").append (invoiceNo).append
(lineSeparator);
buffer.append ("tranId = ").append (tranId).append
(lineSeparator);
return buffer.toString ();
}
}
|