设置Id(PK)生成值自动和手动 [英] set Id (PK) generation value auto and manual
问题描述
我想将用户保存到DB中,并使用IDENTITY生成类型创建的用户的ID(PK)当前场景。
eg
@Entity
@Table(name =USER_PROFILES,uniqueConstraints = @UniqueConstraint(columnNames =USERNAME))
public class UserProfiles实现java.io.Serializable {
private Long id;
私人字符串用户名;
私人字符串密码;
public UserProfiles(){
}
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name =ID,unique = true,nullable = false,precision = 20,scale = 0)
public Long getId(){
返回this.id;
}
public void setId(Long id){
this.id = id;
}
@Column(name =USERNAME,unique = true,nullable = false,length = 32)
public String getUsername(){
return this 。用户名;
}
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;
}
}
但是我想<$ c在以下情况下$ c> Create Id(PK):
1)用户明确设置 Id(PK)
。
2)如果用户没有设置 Id(PK)
,那么它会自动分配,并且它必须是唯一的。
请给我一些可用的选项,以便我可以解决它。
Thanks。
您可以为此定义自定义ID生成器,如 SO Answer
以下是代码的外观: -
@Id
@Basic(可选= false)
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = IdOrGenerated)
@GenericGenerator(name =IdOrGenerated,strategy =... UseIdOrGenerate)
@Column(name =ID,unique = true,nullable = false,precision = 20 ,scale = 0)
public Long getId(){..}
和 p>
public class UseIdOrGenerate extends IdentityGenerator {
@Override
public Serializable generate(SessionImplementor session,Object obj)throws HibernateException {
if(obj == null)抛出新的HibernateException(new NullPointerException()); $()()()()() );
return id;
} else {
return((EntityWithId)obj).getId(); // id不为空,所以使用指定的id。
code $ pre
I want to persist User into DB and current scenario of ID(PK) of User created with IDENTITY generation type.
e.g.
@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;
}
}
but I want to Create Id(PK)
in following scenarios :
1) User sets Id(PK)
explicitly.
2) If User does not set Id(PK)
then it is be assigned automatically and it must be unique.
Please suggest me some available options so I can resolve it.
Thanks.
解决方案 You can define your custom id generator for this purpose as pointed out in this SO Answer
Here is how its code will look like:-
@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="IdOrGenerated")
@GenericGenerator(name="IdOrGenerated",strategy="....UseIdOrGenerate")
@Column(name = "ID", unique = true, nullable = false, precision = 20, scale = 0)
public Long getId(){..}
and
public class UseIdOrGenerate extends IdentityGenerator {
@Override
public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
if (obj == null) throw new HibernateException(new NullPointerException()) ;
if ((((EntityWithId) obj).getId()) == null) {//id is null it means generate ID
Serializable id = super.generate(session, obj) ;
return id;
} else {
return ((EntityWithId) obj).getId();//id is not null so using assigned id.
}
}
}
这篇关于设置Id(PK)生成值自动和手动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!