Java Reference
In-Depth Information
@Entity (access=AccessType.FIELD)
@Table(name="RESTAURANT")
public class Restaurant implements Serializable {
@Id(generate = GeneratorType.AUTO)
private int id;
private String name;
Maps
menuItems
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY)
@JoinColumn(name = "RESTAURANT_ID")
@org.hibernate.annotations.IndexColumn(name="MENU_ITEM_INDEX")
private List<MenuItem> menuItems;
@ManyToMany
private Set<ZipCode> serviceArea;
Maps serviceArea
Finds
ZipCode entity
public boolean isInServiceArea(Address
bbbbbbbbbbbbbbbbbbbbbbbb bb address) {
for (Iterator it = serviceArea.iterator(); it.hasNext();) {
ZipCode zipCode = (ZipCode) it.next();
if (zipCode.getZipCode().equals(address.getZip()))
return true;
}
return false;
}
The menuItems field is a collection of MenuItem objects and is mapped using the
@OneToMany annotation. The serviceField is a set of ZipCode entity beans instead
of a set of strings, and the isInServiceArea() method looks for a ZipCode entity
bean for the specified zipCode . As you can see, this change impacts this class's
constructor and the isInServiceArea() method. It also affects the classes that
create restaurants.
We must also define the ZipCode entity bean, which wraps the ZIP code:
@Entity (access=AccessType.FIELD)
public class ZipCode implements Serializable {
@Id
public String zipCode;
ZipCode() {
}
public ZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getZipCode() {
Search WWH ::




Custom Search