Java Reference
In-Depth Information
@Override
public
public String toString () {
return
return getFullName ();
}
@Transient /* synthetic: cannot be used in JPA queries. */
public
public String getFullName () {
StringBuilder sb = new
new StringBuilder ();
iif ( firstName != null
null )
sb . append ( firstName ). append ( ' ' );
iif ( lastName != null
null )
sb . append ( lastName );
iif ( sb . length () == 0 )
sb . append ( "NO NAME" );
return
return sb . toString ();
}
}
The @Entity annotation at class level directs JPA to treat this as a data object to be mapped
into the database. The @Id informs JPA that this id is the primary key property, and the
@GeneratedValue tells it how to assign the primary key values for newly created objects.
The @Column annotation is only needed when the column name in the relational database dif-
fers from the expected name based on the property; in this case, the SQL database designer
has used surname , whereas the Java developer wants to use lastName .
I said that annotations are class-like things, and so, you can define your own. The syntax here
is a bit funky; you use @interface . It is rumored that the team developing this feature was
either told not to, or was afraid to, introduce a new keyword into the language, due to the
trouble that doing so had caused when the enum keyword was introduced in Java SE 1.4. Or,
maybe they just wanted to use a syntax that was more reminiscent of the annotation's usage.
At any rate, Example 23-16 is a trivial example of a custom annotation.
Example 23-16. Trivial annotation defined
package lang;
public @interface MyToyAnnotation {
}
Annotations are “class-like things” so they should be named the same way—that is, names
that begin with a capital letter and, if public, stored in a source file of the same name (e.g,
MyToyAnnotation.java ).
Search WWH ::




Custom Search