Well, readin a little further, others seem to have come to the
conclusion that your object is not fine, which the more I think about the
more I agree with. (Doesn't deserialization use a default constructor?)
But in any case, how do others thing about using a comparison of
an object to its serialized and deserialized form instead of to a second
object? I guess it wouldn't catch situations like this, but it would be
the "most accurate" test of whether equal objects are .equals().
Aaron
On Mon, 30 Oct 2000, Aaron Mulder wrote:
> Yes, I see. We test whether two objects created with the default
> constructor are equal, since usually all the variables are initialized to
> default values in the deafult constructor. However, in your object, the
> default constructor actually grabs an ID from the database, so two objects
> created by the default constructor are *not* equal, and our test fails
> even though the object is legit.
> So, there are a couple points:
>
> - Your object is fine; you can ignore the message
> - Our test is flawed, because we assume things about the default
> constructor
> - However, we haven't thought of a better test - unless perhaps we were
> to create an object, and then serialize and deserialize it to get another
> object, and then compare the two. Perhaps that's the way to go. Anyone
> have thoughts on this?
>
> Aaron
>
> On Mon, 30 Oct 2000, Tim Squires wrote:
> > Hey Chaps,
> >
> > I have the following errors appearing on deployment of an entity EJB with a
> > single primary key, the value of the primary key being populated from an
> > Oracle sequence:
> >
> > [Verifier]
> > Class : area_type
> > Section: 9.2.9
> > Warning: The primary key class must override equals().
> >
> > [Verifier]
> > Class : area_type
> > Section: 9.2.9
> > Warning: The primary key class must override hashCode().
> >
> > Using prod-3 on NT with JDK1.3.
> >
> > The primary key code is:
> >
> > package com.mywds.beans;
> >
> > import javax.naming.*;
> > import java.sql.*;
> > import javax.sql.*;
> > import com.mywds.util.*;
> >
> > public class area_typePK implements java.io.Serializable
> > {
> > public Integer id;
> >
> > //Assuming that this bean will be implemented on an Oracle database
> > //it uses the area_typePK_seq to retrieve the next primary key integer.
> > public area_typePK()
> > {
> > //Connect to the database
> > Connection conn = null;
> > try
> > {
> > Context ctx = new InitialContext();
> > DataSource ds = (DataSource)ctx.lookup("jdbc/OraclePool");
> > conn = ds.getConnection();
> >
> > //Find the next free integer from the area_typePK_seq sequence
> > Statement stmt = conn.createStatement();
> > ResultSet query = stmt.executeQuery( "select area_typePK_seq.nextval
> > pk from dual" );
> >
> > if ( query.next() )
> > this.id = new Integer( query.getInt( "pk" ));
> > //else
> > //throw new Exception( "area_typePK: No area_type primary key
> > returned from area_typePK_seq." );
> >
> > query.close();
> > query = null;
> > stmt = null;
> >
> > }
> > catch(NamingException ne)
> > {
> > //throw new Exception( "area_typePK: Hello Tim: Unable to connect to
> > JNDI server to retrieve context: "+ne.toString() );
> > }
> > catch(SQLException se)
> > {
> > //throw new Exception( "area_typePK: Problem executing SQL:
> > "+se.toString() );
> > }
> > finally
> > {
> > if(conn != null)
> > try
> > {
> > conn.close();
> > conn = null;
> > }
> > catch(SQLException e)
> > {
> > //throw new Exception( "area_typePK: Unable to close current
> > connection: "+e.toString() );
> > }
> > }
> >
> > }
> >
> > public area_typePK( int id )
> > {
> > this.id = new Integer( id );
> > }
> >
> > public area_typePK( Integer id )
> > {
> > this.id = id;
> > }
> >
> >
> > public int hashCode()
> > {
> > Integer hc = new Integer( id.toString() );
> > return hc.intValue();
> > }
> >
> > public boolean equals( Object obj )
> > {
> > if ( obj == null || !(obj instanceof area_typePK))
> > return false;
> > else if (((area_typePK)obj).id == id )
> > return true;
> > else
> > return false;
> > }
> >
> > public String toString()
> > {
> > return id.toString();
> > }
> >
> > }
> >
> >
> > If I change equals to always return true and the hashCode to always return 1,
> > jBoss does not throw the above error.
> >
> > Any help, hints or examples would be great.
> >
> > Thanks for your time,
> > Tim.
> >
> > ---------------------
> > It's not what you know, it's who you tell.
> >
> > -----------------------
> > "Free text messages!!! Another 100% free service brought to you by the award
>winning Totalise"
> > Visit http://sms.totalise.net"
> >
> >
> >
> >
> > --
> > --------------------------------------------------------------
> > To subscribe: [EMAIL PROTECTED]
> > To unsubscribe: [EMAIL PROTECTED]
> > Problems?: [EMAIL PROTECTED]
> >
>
>
>
> --
> --------------------------------------------------------------
> To subscribe: [EMAIL PROTECTED]
> To unsubscribe: [EMAIL PROTECTED]
> Problems?: [EMAIL PROTECTED]
>
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]