@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 table will be deleted(CascadeType.REMOVE). If a Customer instance is reattached to persistence storage, any changes to the Address collection will be merged with persistence storage (CascadeType.MERGE).
@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 );
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 run: [java] 2004-10-07 14:39:23,103 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo rt: org/apache/axis/AxisFault [java] Air France customers [java] Bill [java] Monica [java] USAir customers [java] Molly
The INFO message you can ignore. It will be fixed in later releases of JBoss 4.0.