JBoss.org Community Documentation

Chapter 24. Introduction to relationships between EJB3 entities

The "entity" tutorial only showed one-to-many and many-to-one relationships. This tutorial will show you one-to-one and many-to-many relationships.

One-to-One :

There is a unidirectional one-to-one relationship defined between the org.jboss.tutorial.relationships.bean.Customer and org.jboss.tutorial.relationships.bean.Address. Customer defines the uni-directional relationship.

			
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "ADDRESS_ID")
public Address getAddress()
{
   return address;
}

			
		

CascadeType.ALL specifies that when a Customer is created, if there is any Address association, then that Address will be created as well(CascadeType.PERSIST). If the Customer is deleted from persistence storage, the Address will be deleted(CascadeType.REMOVE). If a Customer instance is re-attached to persistence storage, any changes to the Address collection will be merged with persistence storage (CascadeType.MERGE).

Many-To-Many :

There is a many-to-many relationship between org.jboss.tutorial.relationships.bean.Customer and org.jboss.tutorial.relationships.bean.Flight. In order to have a many-to-many relationship there needs to be a distinct join table that maps the many-to-many relationship. This is called an association table. You can have JBoss automatically generate the association table for you, or you can use the @JoinTable annotation to define it yourself. If you use @JoinTable it must be defined on both sides of the bi-directional relationship. Let's look at the Customer side of the relationship:

			 	
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy="customers")
			 	
			 

The mappedBy attribute states that the Flight.customers property is responsible for mapping and managing the relationship. The spec allows for multiple join and inverse join columns. See the "Composite Primary Key" tutorial for more information.

Let's look at the other side of the relationship in Flight.

				
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(table = @Table(name = "flight_customer_table"),
                  joinColumns = {@JoinColumn(name = "FLIGHT_ID")},
                  inverseJoinColumns = {@JoinColumn(name = "CUSTOMER_ID")})
public Set<Customer> getCustomers()
{
   return customers;
}

				
			

The database associate table will look like this:

				
create table FLIGHT_CUSTOMER_TABLE (
      CUSTOMER_ID integer,
      FLIGHT_ID integer
   );
				
			

Building and Running

From the command prompt, move to the "relationships" folder under the Section 1.3, “Set the EJB3_TUTORIAL_HOME”

Ant Users:

Make sure your JBossAS-5.x is running

			
$ ant
$ ant run

run:
     [java] Air France customers
     [java] Bill
     [java] Monica
     [java] USAir customers
     [java] Molly



		     
			

Maven Users: Make sure the AS is not running.

$ mvn clean install -PRunSingleTutorial
			

View the tables and rows:

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.