一、实体类Arms
@Entity
public class Arms {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer type;
private Integer quality;
@ManyToOne
private Player player;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Integer getQuality() {
return quality;
}
public void setQuality(Integer quality) {
this.quality = quality;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
二、实体类Player
@Entity
public class Player {
@Id
@GeneratedValue
@Column(length = 36)
private Integer Id;
@Column(length = 10, name = "user_id")
private Integer userId;
private String name;
@Column(length = 6)
private String gender;
@Column(length = 10)
private String password;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
三、关键点说明
@ManyToOne 注解, 多对一。
@Id 注解, 声明实体类的属性映射为数据库的主键列。
@GeneratedValue 注解,用于标注主键的生成策略,通过strategy 属性指定。默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略。
@GeneratedValue(strategy = GenerationType.AUTO) //自动选择主键生成策略
@GeneratedValue(strategy = GenerationType.IDENTITY) //采用数据库ID自增长的方式来自增主键字段,Oracle 不支持这种方式
@GeneratedValue(strategy = GenerationType.SEQUENCE) //通过序列产生主键,通过@SequenceGenerator 注解指定序列名,MySql不支持这种方式
@GeneratedValue(strategy = GenerationType.TABLE) //通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。