Java Reference
In-Depth Information
@OneToMany(mappedBy = "customer")
private Collection<Telephone> telephoneCollection;
@OneToMany(mappedBy = "customer")
private Collection<CustomerOrder> customerOrderCollection;
@OneToMany(mappedBy = "customer")
private Collection<Address> addressCollection;
//generated constructors and methods omitted 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. We can also see that no primary key generation
strategy is used; we either need to populate the primary key ourselves, or add the
@GeneratedValue annotation manually. The @Basic annotation is used to mark
this field as nonoptional.
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
( CUSTOMERS ), but it makes sense to name our entities in the singular ( Customer ).
Additionally, the standard naming convention for database tables that contain
more than one word is to use underscores to separate the words ( CUSTOMER_ORDER ),
whereas in Java the standard is to use camel case ( CustomerOrder ). The @Table
annotation allows us to follow the established naming standards in both the
relational database and the Java world.
Named queries and JPQL
Notice the @NamedQueries annotation in the generated code (this annotation is only
generated if we click on the Generate Named Query Annotations for Persistent
Fields checkbox of the New Entity Classes from Database wizard), this annotation
contains a value attribute (the attribute name can be omitted from the code since
it is the only attribute in this annotation). The value of this attribute is an array of
@NamedQuery annotations. The @NamedQuery annotation has a name attribute, which
is used to give it a logical name (by convention, the JPA entity name is used as part
of the query name, and we can see in the generated code that the New Entity Classes
from Database wizard follows this convention), and a query attribute, which is used
to define a JPQL query to be executed by the named query.
Search WWH ::

Custom Search