Java Reference
In-Depth Information
query = "SELECT c FROM Customer c WHERE c.customerId = :customerId"),
@NamedQuery(name = "Customer.findByFirstName", query = "SELECT c FROM
Customer c WHERE c.firstName = :firstName"),
@NamedQuery(name = "Customer.findByMiddleName", query = "SELECT c FROM
Customer c WHERE c.middleName = :middleName"),
@NamedQuery(name = "Customer.findByLastName", query = "SELECT c FROM
Customer c WHERE c.lastName = :lastName"),
@NamedQuery(name = "Customer.findByEmail", query = "SELECT c FROM
Customer c WHERE c.email = :email")})
public class Customer implements Serializable {
@Id
@Column(name = "CUSTOMER_ID", nullable = false)
private Integer customerId;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "MIDDLE_NAME")
private String middleName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "EMAIL")
private String email;
@OneToMany(mappedBy = "customerId")
private Collection<CustomerOrder> customerOrderCollection;
@OneToMany(mappedBy = "customerId")
private Collection<Address> addressCollection;
@OneToMany(mappedBy = "customerId")
private Collection<Telephone> telephoneCollection;
//Getters, setters and other generated methods and
//constructors removed for brevity.
}
As we can see, NetBeans generates a class decorated with the @Entity annotation,
which marks the class as a JPA entity. Notice that NetBeans automatically decorated
one of the fields with the @Id annotation, based on the primary key constraint in
the table used to generate the JPA entity. Notice that no primary key generation
strategy is used. We either need to populate the primary key ourselves, or add the
@GeneratedValue annotation manually.
Also notice the @Table annotation. This is an optional annotation that indicates what
table our JPA entity maps to. If the @Table annotation is not used, then our entity
will map to a table having the same name as the entity class (case insensitive). In our
particular example, the @Table annotation is redundant, but there are cases where
it is useful. For example, some database schemas have tables named in plural
 
Search WWH ::




Custom Search