View RSS Feed

My Java Tips

Sample Entity Bean

Rate this Entry
by , 11-05-2011 at 06:45 PM (624 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:
@Entity
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) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@OneToMany
public Collection getOrders() {
return orders;
}
public void setOrders(Collection orders) {
this.orders = orders;
}
@ManyToMany
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) {
this.getPhones().add(phone);

// Update the phone entity instance to refer to this customer
phone.addCustomer(this);
}
}

Submit "Sample Entity Bean" to Facebook Submit "Sample Entity Bean" to Digg Submit "Sample Entity Bean" to del.icio.us Submit "Sample Entity Bean" to StumbleUpon Submit "Sample Entity Bean" to Google

Tags: None Add / Edit Tags
Categories
EJB

Comments