• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bodgit and Scraper Transfer object

 
mohini lalwani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ALL ,

I created contractor object for Bodgit and Scraper Version 2.3.1.

I define equals and hashCode methods .
Subcontractor name will be unique per contractor
Please give your reviews . Did they look okay ?

/**
* Checks whether two Contractor objects are the same by comparing their
* SubContractor name. Since a SubContractor name is unique per Contractor,
* if the two SubContractor names are the same, the two Contractor records
* must be the same object. If we could not use the SubContractor name alone
* to compare two Contractor records, we might have to compare more fields
* within the two records.
*
* @param contractor
* the Contractor to compare with this Contractor.
* @return true if this Contractor and the other Contractor have the same
* SubContractor name.
*/
public boolean equals(Object contractor) {
if (!(contractor instanceof Contractor)) {
return false;
}

Contractor otherContractor = (Contractor) contractor;

return (subcontractorName == null) ? (otherContractor
.getSubcontractorName() == null) : subcontractorName
.equals(otherContractor.getSubcontractorName());
}

/**
* Returns a hashcode for this Contractor object that should be reasonably unique
* amongst Contractor objects. As with the <code>equals</code> method, we know
* that the SubContractor name will be unique amongst Contractor records, so all we need do is
* return the SubContractor name hashcode. If the SubContractor name did not provide a reasonably unique
* value, we might consider generating a more unique hashcode (possibly by
* adding all the hashcodes of all the values in this object).
*
* @return the hashcode for this instance of Contractor.
*/
public int hashCode() {
return subcontractorName.hashCode();
}
 
Justin Rundle
Ranch Hand
Posts: 123
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the contractor's name is unique...?
 
Oyvind Gjare
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I agree with Justin, I don't think you can assume that the name should be unique.

I work on the same (or a similar) task, and I am also going to use an entity object for the contractor. But as I see it, the whole record shold be considered to be unique.

A certain company A offers service B in the city C, charging $ D, and a customer E might have booked that service.

By no means you can say that the name of the company is the primary key. If you need a primary key (or, that is, something to check if this record equals another record),
I would add a field to the contractor object that contains the database index. As long as the records can't be removed from the DB (only deleted logically) this index will not change.

Oyvind
 
mohini lalwani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

I am trying to implement
public String [] read(int recNo) throws RecordNotFoundException;

method of DBMain interface .

recNo should be primary key of each record
Should I put this field in Contractor object ?

Please give your inputs .
 
mohini lalwani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Justin and Oyvind Gjare

Then should I put recNo field as primary key
then equals and hashCode method of Contractor object will have recNo field.
is that okay ?

public boolean equals(Object contractor) {
if (!(contractor instanceof Contractor)) {
return false;
}

Contractor otherContractor = (Contractor) contractor;

return (recNo== null) ? (otherContractor
.getRecNo() == null) : subcontractorName
.equals(otherContractor.getRecNo() );
}
----------------------------------------------------------------------
public int hashCode() {
return recNo.hashCode();
}


this time recNo is unique for each record .

please give your inputs
 
Oyvind Gjare
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mohini,

well, I've opted for using the recNo in the contractor class. My contractor class has the following attributes:

private int dbKey;
private String name;
private String location;
private String specialties;
private Integer size;
private String rate;
private String owner;

I had a look at the class though, and to be honest, I have implemented a hashCode method and an equals methods that use all the attributes, But I can't see why this should be necessary. The database description seems to indicate that records will only be marked as deleted, not actually deleted, so a primary key based on this should be fine for this application.

But the question is why you need to compare the instances at all. Personally, I just use the contractor class to put some input checks (owner should be 6 digits...) and to find the DB index when saving the changes, since the my JTable has no such column.

I have to admit that I've just started to take the certification, so I would be glad to hear other opinions on this. Are there any need for hashCode() and equals() at all (apart from the fact that the instances are probably put in a map and need to be kept from one another)? Or do any of you even omit to use a class to represent the database record (that is, use only the String[] all through the application)?

Oyvind
 
Karen Smrha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohini,

I also have B&S 2.3.1. The database file I received from SUN
does NOT have unique contractor names. I don't recommend
using name alone as your primary key in case they test
you system with a database file like mine.

I have chosen to use the recordId as my primary key and
I store this in my contractor value object. This is a
simple solution. Some people use a composite primary key
made of of name/location or name/location/specialties.

Good luck,
Karen Smrha
 
Tom Doyle
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following the same strategy as Karen by using the recordID as my unique identifier. This would appear to be the simpliest and most intuitive available approach.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic