JBoss.org Community Documentation

Chapter 26. Introduction to Secondary tables for EJB3 entities

The EJB specification allows you to map an entity bean to multiple tables. You do this by using the @SecondaryTable annotation.

The org.jboss.tutorial.secondary.bean.Customer entity maps its address properties to a separate ADDRESS table. The first thing it does is define the secondary table.

			
@Entity
@Table(name = "CUSTOMER")
@SecondaryTable(name = "EMBEDDED_ADDRESS", join = {@JoinColumn(name = "ADDRESS_ID")})
public class Customer implements java.io.Serializable
{
}
			
		

The @JoinColumn of the secondary table must match the value of the Customer's primary key. To map individual properties to a secondary table you use the secondaryTable member value of @Column.

			
@Column(name = "STREET", secondaryTable = "EMBEDDED_ADDRESS")
public String getStreet()
{
   return street;
}

			
		

Building and Running

From the command prompt, move to the "secondary" 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] Create Bill Burke and Monica Smith
     [java] Bill and Monica get married
     [java] Get all the Burkes
     [java] There are now 2 Burkes



		     
			

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.