Version 2

    Merging and Basic Queries

    This example shows a bunch of things.  First, it introduces the @Column annotation.  It also shows how entities can be detached and reattached 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 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 CustomerDAOBean stateless EJB's find() method wrapps 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.  Also, queries also support named parameters now.  The 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).listResults();



    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 Client does.


    Building and Running

    To build and run the example, make sure you have ejb3.deployer installed in JBoss 4.0.x and have JBoss running.  See the reference manual on how to install EJB 3.0. 

    Unix:    $ export JBOSS_HOME=<where your jboss 4.0 distribution is>
    Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is>
    $ ant
    $ ant run
         [java] Create Bill Burke and Monica Smith
         [java] 2004-10-06 22:27:50,344 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo
    rt: org/apache/axis/AxisFault
         [java] Bill and Monica get married
         [java] Get all the Burkes
         [java] There are now 2 Burkes


    The INFO message you can ignore.  It will be fixed in later releases of JBoss 4.0.


    View the tables and rows

    You can view the tables created by JBoss by going to the Hypersonic SQL 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.


    Jar structure

    EJB 3.0 beans must be packaged in a JAR file with the suffix .ejb3.  Running the ant script above creates a JAR file within the deploy/ directory of JBoss.  All that needs to be in that jar is your server-side class files and additionally, you will probably need to define a file in the META-INF directory of the JAR. is needed if you need to hook in a datasource other than JBoss's DefaultDS, or change the caching of Hibernate.  See the EJB 3.0 reference manual and Hibernate reference manual for more details.f