JBoss.org Community Documentation

Chapter 22. Introduction to merging and querying of entities in EJB3

This example shows a bunch of things. First, it introduces the @Column annotation. It also shows how entities can be detached and re-attached to persistence storage using the EntityManager.merge(). It also shows some basic queries.

@Column :

EJB 3.0 has a complete Object/Relational mapping. You can use the @Column annotation to specify which column in the table your property should map to. Take a look at the org.jboss.tutorial.merge.bean.Customer entity.

				
@Column(name = "FIRST")
public String getFirst()
{
   return first;
}

				
			

Find by primary key :

The EntityManager service has a built in find by primary key method:

				
<T> find(Class<T>, Object pk)
				
			

The org.jboss.tutorial.merge.bean.CustomerDAOBean stateless EJB's find() method wraps remote calls to the EntityManager.

				
public Customer find(int id)
{
   return manager.find(Customer.class, id);
}

				
			

Queries :

EntityManager allows you to create query objects on the fly that can be reused over and over, or just one time. Queries also support named parameters. The org.jboss.tutorial.merge.bean.CustomerDAOBean reflects this usage in the findByLastName method.

				
public List findByLastName(String name)
{
   return manager.createQuery("from Customer c where c.last = :name").setParameter("name", name).getResultList();
}