我想将用户持久保存到数据库和使用 IDENTITY 生成类型创建的用户 ID(PK) 的当前场景中。例如
@Entity
@Table(name = "USER_PROFILES", uniqueConstraints = @UniqueConstraint(columnNames = "USERNAME"))
public class UserProfiles implements java.io.Serializable {
private Long id;
private String username;
private String password;
public UserProfiles() {
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false, precision = 20, scale = 0)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "USERNAME", unique = true, nullable = false, length = 32)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "PASSWORD", nullable = false, length = 32)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
但我想Create Id(PK)
在以下情况下:1)用户Id(PK)
明确设置。2)如果用户没有设置Id(PK)
,那么它是自动分配的,它必须是唯一的。
请建议我一些可用的选项,以便我可以解决它。谢谢。