Java Reference
In-Depth Information
The following listing shows a simple example, based on a single persistent class called
Product .
Listing 8.1. The Product class, a POJO mapped to a database table
package mjg;
public class Product {
private int id;
private String name;
private double price;
public Product() {}
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
The Product class has only three attributes, one of which ( id ) will represent the primary
key in the database. The rest of the class is simply constructors, getters and setters, and (not
shown) the normal toString , equals , and hashCode overrides. The complete ver-
sion is available in the topic source code.
The next listing shows the ProductDAO interface.
Listing 8.2. A DAO interface for the Product class
import java.util.List;
public interface ProductDAO {
List<Product> getAllProducts();
Product findProductById(int id);
void insertProduct(Product p);
void deleteProduct(int id);
}
Search WWH ::




Custom Search