Java Reference
In-Depth Information
public Integer getNumber() {
return this.number;
}
public void setNumber(Integer number) {
this.number = number;
}
public void setCode(String code) {
this.code = code;
}
public int hashCode() {
int hashCode = 0;
if( code != null ) hashCode ^= code.hashCode();
if( number != null ) hashCode ^= number.hashCode();
return hashCode;
}
public boolean equals(Object obj) {
if( !(obj instanceof AccountPk) ) return false;
AccountPk target = (AccountPk)obj;
return ((this.code == null) ?
(target.code == null) :
this.code.equals(target.code))
&& ((this.number == null) ?
(target.number == null) :
this.number.equals(target.number));
}
}
}
The next most natural approach is the use of the @EmbeddedId tag. Here, the primary
key class cannot be used in other tables since it is not an @Embeddable entity, but it does
allow us to treat the key as a single attribute of the Account class (in Listings 6-7 and 6-8,
the implementation of AccountPk is identical to that in Listing 6-6, and is thus omitted for
brevity). Note that in Listings 6-7 and 6-8, the AccountPk class is not marked as @Embeddable .
Listing 6-7. Using the @EmbeddedId Annotation to Map a Compound Primary Key
package com.hibernatebook.annotations;
import javax.persistence.*;
@Entity
public class Account {
private String description;
private AccountPk id;
Search WWH ::




Custom Search