1、自动增长主键配置:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
适用范围:用于Mysql主键自动增长的情况。
2、SequenceGenerator 配置
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="personPKGen")
@SequenceGenerator(name="personPKGen",sequenceName="PERSON_PK_GEN",allocationSize=1)
private int id;
备注:
SequenceGenerator.name:主键生成器的名称,提供给GeneratedValue.generator属性引用;
SequenceGenerator.sequenceName:对应Oracle数据库的序列名称;
SequenceGenerator.allocationSize:增长值,如果不设定,默认是50;
适用范围:用于Oralcle数据库的序列。
3、TableGenerator配置
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="personPKGen")
@TableGenerator(name="personPKGen", table="id_generator", pkColumnName="gen_name",pkColumnValue="person_PK", valueColumnName="gen_value",allocationSize=1)
备注:
TableGenerator.table:对应主键生成表;
TableGenerator.pkColumnName:主键列名;
TableGenerator.pkColumnValue:主键列值;
4、复合主键配置
(1)定义主键类
public class Name implements Serializable {
private String firstName;
private String lastName;
public Name() {
}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
} //重写hashCode方法
public int hashCode() {
return this.firstName.hashCode() + this.lastName.hashCode();
} //重写equals方法
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.getClass() == obj.getClass()) {
Name name = (Name) obj;
if(name.getFirstName().equals(this.getFirstName())&& name.getLastName().equals(this.getLastName())) {
return true;
}
}
return false;
}
}
(2)可以通过@IdClass和@Id注释的方式配置主键,或者通过 @EmbededId方式配置主键。
- @IdClass 与 @Id配置主键:
@Entity @Table(name = "person_embedded_id") @IdClass(Name.class) public class Person { //多个@Id配置复合ID @Id @Column(name="first_name", nullable=false, length=64) private String firstName; @Id @Column(name="last_name", nullable=false, length=64) private String lastName; @Temporal(value = TemporalType.DATE) private Date birthday; @Column(length=64) private String email; @Column(length=11) private String phone; @Column(name="home_address", length=256) private String homeAddress; @Column(name="company_address", length=256) private String companyAddress; public Person() { } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday){ this.birthday = birthday; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getHomeAddress() { return homeAddress; } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public String getCompanyAddress() { return companyAddress; } public void setCompanyAddress(String companyAddress) { this.companyAddress = companyAddress; } }
- @EmbeddedId配置
@Entity @Table(name = "person_embedded_id") public class Person { @EmbeddedId @AttributeOverrides({ @AttributeOverride(name="firstName", column=@Column(name="first_name", nullable=false, length=64)), @AttributeOverride(name="lastName", column=@Column(name="last_name", nullable=false, length=64)) }) private Name name; @Temporal(value = TemporalType.DATE) private Date birthday; @Column(length=64) private String email; @Column(length=11) private String phone; @Column(name="home_address", length=256) private String homeAddress; @Column(name="company_address", length=256) private String companyAddress; public Person() { } public Name getName() { return name; } public void setName(Name name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday){ this.birthday = birthday; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getHomeAddress() { return homeAddress; } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public String getCompanyAddress() { return companyAddress; } public void setCompanyAddress(String companyAddress) { this.companyAddress = companyAddress; } }