Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

CMP with null values in primary key class
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 ();
}
}

Report this thread to moderator Post Follow-up to this message
Old Post
Arun.S.L
10-14-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Beans archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:58 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.