2. JPA Entity Configuration

Example 1. Excerpt from Person

The javax.persistence annotation @Entity indicates this is a persistent entity bean, and JPA would pick it up during classpath scanning. Although in this configuration, JPA scanning for entity beans is turned off. 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.

[Note]Note

Using the ImprovedNamingStrategy in your JPA persistence.xml can give better translation from camel case classes and field names to standard underscore delimited database names. An example of this is that instead of having to explicitly put @Column(name="FIRST_NAME") on the first name field, it automatically converts the camel case of the field name to use underscores.

                        
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                
                    

Inheritance is configured using the @Inheritance and @DiscriminatorColumn annotations. The @Inheritance configures the inheritance type to joined. The @DiscriminatorColumn annotation sets the field used to determine the subclass to the TYPE field and the field type to integer.

                
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE", discriminatorType=DiscriminatorType.INTEGER)
public class Person implements Serializable {

    private static final long serialVersionUID = -2175150694352541150L;

    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.
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets first name.
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Gets 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, cascade=CascadeType.ALL)
    @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;
    }

    public Address findAddressById(Integer id) {
        Address result = null;

        if (addresses != null) {
            for (Address address : addresses) {
                if (address.getId().equals(id)) {
                    result = address;

                    break;
                }
            }
        }

        return result;
    }

    ...

}
                
            

Example 2. Student

The Student class extends Person. The @DiscriminatorValue indicates the value to be used for the discriminator column in the parent class when storing and retrieving this subclass. The PERSON_STUDENT table stores all the extra values unique to this subclass, which in this case is just the school name of the student.

                
@Entity
@Table(name="PERSON_STUDENT")
@DiscriminatorValue("1")
public class Student extends Person {

    private static final long serialVersionUID = -8933409594928827120L;

    private String schoolName = null;

    /**
     * Gets school name.
     */
    public String getSchoolName() {
        return schoolName;
    }

    /**
     * Sets school name.
     */
    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }
    
}
                
            

Example 3. Professional

The Professional class is very similar to the Student class except it has a different table, discriminator value, and it's unique field is the professional's company name.

                
@Entity
@Table(name="PERSON_PROFESSIONAL")
@DiscriminatorValue("2")
public class Professional extends Person {

    private static final long serialVersionUID = 8199967229715812072L;

    private String companyName = null;

    /**
     * Gets company name.
     */
    public String getCompanyName() {
        return companyName;
    }

    /**
     * Sets company name.
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    
}