2. Hibernate Configuration

The @Entity annotation indicates that the JavaBean is a persistent entity. By default the class name will be used for the Hibernate entity name as this class is registered with Hibernate. A different entity name could be specified by using the annotations name attribute (ex: @Entity(name="Employee")). An @Table annotation can explicitly configure which table the entity is mapped to, although in this case it isn't necessary since it would default to the name of the class which matches the table name.

The one-to-many relationship from Person to Address is configured on public Set<Address> getAddresses(). The @OneToMany(fetch=FetchType.EAGER) annotation indicates a one-to-many relationship and that any associated addresses should be eagerly fetched. The @JoinColumn(name="PERSON_ID", nullable=false) annotation indicates ADDRESS.PERSON_ID is the column to match against PERSON.ID to retrieve addresses associated with a person.

Example 1. Excerpt from Person

                
@Entity
public class Person {

    private Integer id = null;
    private String firstName = null;
    private String lastName = null;
    private Set<Address> addresses = null;
    private Date created = null;
    
    /**
     * Gets id (primary key).
     */
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    /**
     * Sets id (primary key).
     */
    public void setId(Integer id) {
        this.id = id;
    }
    
    /**
     * Gets first name.
     */
    @Column(name="FIRST_NAME")
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets first name.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    /**
     * Gets last name.
     */
    @Column(name="LAST_NAME")
    public String getLastName() {
        return lastName;
    }
    
    /**
     * Sets last name.
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Gets list of <code>Address</code>es.
     */
    @OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name="PERSON_ID", nullable=false)
    public Set<Address> getAddresses() {
        return addresses;
    }

    /**
     * Sets list of <code>Address</code>es.
     */
    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }

    /**
     * Gets date created.
     */
    public Date getCreated() {
        return created;
    }

    /**
     * Sets date created.
     */
    public void setCreated(Date created) {
        this.created = created;
    }

...


}
                
            

Example 2. Excerpt from Address

                
@Entity
public class Address {

    private Integer id = null;
    private String address = null;
    private String city = null;
    private String state = null;
    private String zipPostal = null;
    private Date created = null;
    
    /**
     * Gets id (primary key).
     */
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    /**
     * Sets id (primary key).
     */
    public void setId(Integer id) {
        this.id = id;
    }
    
    /**
     * Gets address.
     */
    public String getAddress() {
        return address;
    }

    /**
     * Sets address.
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * Gets city.
     */
    public String getCity() {
        return city;
    }

    /**
     * Sets city.
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * Gets state.
     */
    public String getState() {
        return state;
    }

    /**
     * Sets state.
     */
    public void setState(String state) {
        this.state = state;
    }

    /**
     * Gets zip or postal code.
     */
    @Column(name="ZIP_POSTAL")
    public String getZipPostal() {
        return zipPostal;
    }

    /**
     * Sets zip or postal code.
     */
    public void setZipPostal(String zipPostal) {
        this.zipPostal = zipPostal;
    }

    /**
     * Gets date created.
     */
    public Date getCreated() {
        return created;
    }

    /**
     * Sets date created.
     */
    public void setCreated(Date created) {
        this.created = created;
    }

...

}