News, Software & Java Forum Index


All times are GMT - 4 Hours
Author Message

<  Java  ~  CMP with null values in primary key class

arunsl22
Posted: Thu Oct 14, 2004 12:00 am Reply with quote
. .
Joined: 13 Oct 2004 Posts: 2
I have a composite primary key in database consisting of 5 fields. Two of the fields are null. I need to implement this in my primary key class for a CMP. Can anyone help me please
View user's profile Send private message
QiSoftware
Posted: Thu Oct 14, 2004 3:01 pm Reply with quote
Admin Joined: 14 Sep 2004 Posts: 184 Location: USA
Hi --

I am a little confused by your question. If you are using JDBC and Java the SQL command to create a primary key is discussed at this link --> SQL unique key tutorial. The command discussed in the tutorial would be executed as any SQL command would be in your program. Let me know if I misunderstood.

Q...

_________________
Business Directory :: Hosting-Q :: It's Personal
View user's profile Send private message Visit poster's website
arunsl22
Posted: Fri Oct 15, 2004 3:00 am Reply with quote
. .
Joined: 13 Oct 2004 Posts: 2
Hi,
I have created a Container Managed Persistence bean. To access the record from a table consisting of a composite primary key of 5 fields, I have a primary key class. It is as shown below.
Among which invoiceNo and tranId is null. I get error when i pass null or zero values in the hashcode and equals function.

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 ();
}
}

Please help me out

Regards,
Arun.S.L
View user's profile Send private message

Display posts from previous:  

WiredPages
Business Directory
Automotive
Business & Financial
Technology
Health Services
Homes & Services
Dining & Travel
Shops & Services
Entertainment & Media
Home
Site Submission





Page 1 of 1
Post new topic

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum