Example 1. Excerpt from Person
The @Entity
annotation indicates that the JavaBean is a persistent entity. JPA would automatically
pick up this class except the JPA configuration explicitly registers the classes each persistence unit should handle.
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 | |
---|---|
Using the <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/> |
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.
@Entity public class Person implements Serializable { private static final long serialVersionUID = -8712872385957386182L; 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; } ... }
Example 2. Excerpt from Address
@Entity public class Address implements Serializable { private static final long serialVersionUID = 7851794269407495684L; private Integer id = null; private String address = null; private String city = null; private String state = null; private String zipPostal = null; private String country = 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. */ public String getZipPostal() { return zipPostal; } /** * Sets zip or postal code. */ public void setZipPostal(String zipPostal) { this.zipPostal = zipPostal; } /** * Gets country. */ public String getCountry() { return country; } /** * Sets country. */ public void setCountry(String country) { this.country = country; } /** * Gets date created. */ public Date getCreated() { return created; } /** * Sets date created. */ public void setCreated(Date created) { this.created = created; } ... }