View RSS Feed

My Java Tips

Sample Entity Bean

Rate this Entry
by , 11-05-2011 at 07:45 PM (947 Views)
Creating entity bean is simple. You have to annotate the class to indicate that itís an entity bean. Also your class should implement Serializable interface. An example entity bean is presented below. Do note that we have defined a no argument constructor which is a must.

Java Code:
public class Customer implements Serializable {

private Long id;
private String name;
private Address address;
private Collection orders = new HashSet();
private Set phones = new HashSet();

// No-arg constructor
public Customer() {}

@Id // property access is used
public Long getId() {
return id;
public void setId(Long id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;
public Address getAddress() {
return address;
public void setAddress(Address address) {
this.address = address;
public Collection getOrders() {
return orders;
public void setOrders(Collection orders) {
this.orders = orders;
public Set getPhones() {
return phones;
public void setPhones(Set phones) {
this.phones = phones;

// Business method to add a phone number to the customer
public void addPhone(PhoneNumber phone) {

// Update the phone entity instance to refer to this customer

Submit "Sample Entity Bean" to Facebook Submit "Sample Entity Bean" to Digg Submit "Sample Entity Bean" to Submit "Sample Entity Bean" to StumbleUpon Submit "Sample Entity Bean" to Google

Tags: None Add / Edit Tags