Java Reference
In-Depth Information
public Account(String description) {
this.description = description;
}
protected Account() {
}
@EmbeddedId
public AccountPk getId() {
return this.id;
}
public String getDescription() {
return this.description;
}
public void setId(AccountPk id) {
this.id = id;
}
public void setDescription(String description) {
this.description = description;
}
public static class AccountPk {
// ...
}
}
Finally, the use of the @IdClass and @Id annotations allows us to map the compound pri-
mary key class using properties of the entity itself corresponding to the names of the properties
in the primary key class. The names must correspond (there is no mechanism for overriding
this), and the primary key class must honor the same obligations as with the other two tech-
niques. The only advantage to this approach is its ability to “hide” the use of the primary key
class from the interface of the enclosing entity. The @IdClass annotation takes a value parame-
ter of Class type, which must be the class to be used as the compound primary key. The fields
that correspond to the properties of the primary key class to be used must all be annotated
with @Id —note in Listing 6-8 that the getCode() and getNumber() methods of the Account class
are so annotated, and the AccountPk class is not mapped as @Embeddable , but it is supplied as
the value of the @IdClass annotation.
Search WWH ::




Custom Search