Skip to Content
Enterprise JavaBeans, Third Edition
book

Enterprise JavaBeans, Third Edition

by Richard Monson-Haefel
September 2001
Intermediate to advanced
592 pages
18h 22m
English
O'Reilly Media, Inc.
Content preview from Enterprise JavaBeans, Third Edition

Chapter 15. Design Strategies

The previous 14 chapters have presented the core EJB technology. What’s left is a grab bag of miscellaneous issues: how to solve particular design problems, how to work with particular kinds of databases, and topics of that nature.

Hash Codes in Compound Primary Keys

Chapter 11 discusses the necessity of overriding the Object.hashCode() and Object.equals() methods in the primary key class of an entity bean. With complex primary keys that have several fields, overriding Object.equals() is fairly trivial. However, overriding Object.hashCode() is more complicated, because an integer value that can serve as a suitable hash code must be created from several fields.

One solution is to concatenate all the values into a String and use the String object’s hashCode() method to create a hash code value for the whole primary key. The String class has a decent hash code algorithm that generates a fairly well-distributed and repeatable hash code value from any set of characters.

The following code shows how to create such a hash code for a hypothetical primary key:

public class HypotheticalPrimaryKey implements java.io.Serializable { public int primary_id; public short secondary_id; public java.util.Date date; public String desc; public int hashCode() { StringBuffer strBuff = new StringBuffer(); strBuff.append(primary_id); strBuff.append(secondary_id); strBuff.append(date); strBuff.append(desc); String str = strBuff.toString(); int hashCode = str.hashCode(); return ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Expert One-on-One™ J2EE™ Development without EJB™

Expert One-on-One™ J2EE™ Development without EJB™

Rod Johnson, Juergen Hoeller
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding
EJB™ 2.1 Kick Start

EJB™ 2.1 Kick Start

Peter Thaggard
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer

Publisher Resources

ISBN: 0596002262Catalog PageErrata