使用 @Embeddable 標註,實現 Serializable,覆寫 equals() 和 hashCode()。
@Embeddable
public class F {
private int id;
private int id2;
// getter/setter
}
使用 @IdClass(F.class) 指定主鍵類,各主鍵字段標註 @Id,字段名需與主鍵類一致。
@Entity
@Table(name="E_USER", uniqueConstraints={@UniqueConstraint(columnNames={"yahoo"})})
@IdClass(F.class)
public class User {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id2;
private String yahoo;
// getter/setter
}
create table E_USER (
id integer not null,
id2 integer not null,
yahoo varchar(255),
primary key (id, id2),
unique (yahoo)
)
@Table 定義唯一約束