JBoss.org Community Documentation
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.
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; }
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); }
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(); }
The Value Object pattern is built into EJB 3.0. You can detach an object from persistence storage and send it across
the network to the client. The client can make updates locally to the object, send it back to the server and the changes
can be merged/synchronized back to the database using the EntityManager.merge()
method.
This is exactly what the org.jboss.tutorial.merge.client.Client
does.
To build and run the example, make sure you have installed JBoss 5.x. See the Section 1.1, “JBoss Application Server 5.x” for details.
From the command prompt, move to the "merge" folder under the Section 1.3, “Set the EJB3_TUTORIAL_HOME”
Make sure your JBossAS-5.x is running
$ ant $ ant run run: [java] Create Bill Burke and Monica Smith [java] Bill and Monica get married [java] Get all the Burkes [java] There are now 2 Burkes
$ mvn clean install -PRunSingleTutorial
You can view the tables created by JBoss by going to the
Hypersonic Service,
scrolling down to the startDatabaseManager
button and clicking it.
A Hypersonic SQL window will be minimized, but you can open it up to look at the tables and do queries.