Java Reference
In-Depth Information
}
public void edit(Customer customer) {
em.merge(customer);
}
public void remove(Customer customer) {
em.remove(em.merge(customer));
}
public Customer find(Object id) {
return
em.find(
com.ensode.ejbdao.entities.Customer.class, id);
}
public List<Customer> findAll() {
return em.createQuery(
"select object(o) from Customer as " +
"o").getResultList();
}
}
As we can see, the generated session bean is not much more than a facade to
EntityManager . Wrapping its calls inside a session bean gives us all of its
advantages, such as transaction management and distributed code. The generated
create() method is used to create new entities, the edit() method updates an
existing entity, the remove() method deletes an existing entities. The find() method
finds an entity with the given primary key, and the findAll() method returns a
List of all entities in the database.
We are, of course, free to add additional methods to the generated session bean. For
example, sometimes it is necessary to add a method to find all entities that meet a
specific criteria, such as finding all customers with the same last name.
One disadvantage of adding methods to the generated session bean
is that if for any reason they need to be regenerated, we will lose our
custom methods and they will need to be re-added. In order to avoid this
situation, it is a good idea to extend the generated session beans and add
additional methods in the child classes (as of Java EE 5, session beans can
extend one another). This will prevent our methods from being "wiped
out" if we ever need to regenerate our session beans.
 
Search WWH ::




Custom Search