Home
Account entity
Example | -5 shows an extract of the Account class.
Example 12-5 Account entity
package itso.bank.entities;
import java.io.Serializable;import java.math.BigDecimal;import java.util.Set;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.OneToMany;
@Entitypublic class Account implements Serializable {@Idprivate String id;
private BigDecimal balance;
@OneToMany(mappedBy="account")private Set<Transaction> transactCollection;
@ManyToMany@JoinTable(joinColumns=@JoinColumn(name="ACCOUNT_ID"),inverseJoinColumns=@JoinColumn(name="CUSTOMER_SSN"))private Set<Customer> customerCollection;
......// contructor, getter, setter methods
The @Entity annotation defined the class as an entity.
The @Id annotation defines id as the primary key.
The @OneToMany annotation defines the 1:m relationship with Transaction. The mapping is defined in the Transaction entity. A Set<Tranasaction> field holds the related instances.
The @ManyToMany and @JoinTables annotations define the m:m relationship with Customer, including the two join columns. A Set<Customer> field holds the related instances. We have to add the name of the relationship table (ITSO.ACCOUNT_CUSTOMER).
ibm.com/redbooks