[英]Auto Increment Using @ Generated
I am using sql server 2008R2 for my java enterprise app. 我将sql server 2008R2用于我的Java企业应用程序。 Now, I want that while persisting a bean its Id column gets automatically updated.
现在,我希望在保留bean的同时,其ID列会自动更新。 My entity bean is:
我的实体bean是:
@Entity
@Table(name = "BANK_MASTER")
@XmlRootElement
public class BankMaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "BANK_ID")
private Long bankId;
@Size(max = 30)
@Column(name = "BANK_NAME")
private String bankName;
@Size(max = 25)
@Column(name = "IP_ADDRESS")
private String ipAddress;
@Size(max = 255)
@Column(name = "URL")
private String url;
@Size(max = 1)
@Column(name = "FORM_METHOD")
private String formMethod;
@Size(max = 1)
@Column(name = "SECURED")
private String secured;
@Column(name = "ACTIVEFLAG")
private Short activeflag;
@Column(name = "ENABLED")
private Short enabled;
@OneToMany(mappedBy = "bankId")
private Collection<BankBranchMaster> bankBranchMasterCollection;
@JoinColumn(name = "PARTNER_ID", referencedColumnName = "UA_ID")
@ManyToOne
private PartnerAccount partnerId;
}
However when I persist the bean it gives constraint error. 但是,当我坚持豆它给出约束错误。 My table create query is as follows:
我的表创建查询如下:
CREATE TABLE [dbo].[BANK_MASTER](
[BANK_ID] [numeric](10, 0) IDENTITY(105,1) NOT NULL,
[BANK_NAME] [varchar](30) NULL,
[IP_ADDRESS] [varchar](25) NULL,
[URL] [varchar](255) NULL,
[FORM_METHOD] [varchar](1) NULL,
[SECURED] [varchar](1) NULL,
[PARTNER_ID] [numeric](10, 0) NULL,
[ACTIVEFLAG] [numeric](1, 0) NULL,
[ENABLED] [numeric](1, 0) NULL
You've added @NotNull
to the id
property. 您已将
@NotNull
添加到id
属性。 This thus means that JPA will check that this id is not null before persisting. 因此,这意味着JPA将在持久化之前检查该id是否为null。 But the id is null, since it will be generated after, by the database.
但是id为null,因为它将在数据库之后生成。 So, the NotNull annotation is in direct contradiction with the fact that it's auto-generated by identity.
因此,NotNull注释与它由身份自动生成的事实直接矛盾。
Simply remove the @NotNull
annotation from id
. 只需从
id
删除@NotNull
批注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.