简体   繁体   English

使用@ Generated自动递增

[英]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.

相关问题 使用自动生成的密钥从JDBC Derby数据库中删除(始终以IDEGER身份生成始终为INTEGER NOT NULL(以1开头,以1递增)) - Delete from JDBC Derby Database using the auto-generated key (INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)) 使用oracle在hibernate中自动增加 - Auto Increment in hibernate using oracle 为什么JPA persist()不会生成自动增量主ID? - Why JPA persist() does not generated auto-increment primary ID? 休眠自动生成 <generator class=“increment” /> 在hbm文件中 - Hibernate auto generated <generator class=“increment” /> in hbm files mySQL中的ID值为auto_increment,但似乎是在netbeans中生成的 - ID value in mySQL is auto_increment but seems generated in netbeans 生成的@ManyToMany联接表的JPA auto_increment ID - JPA auto_increment id for generated @ManyToMany join table Hibernate PostgreSQL 自动生成的增量代码字段有什么乱七八糟的? - Hibernate PostgreSQL what a mess with auto generated increment code field? 使用不同的数据库自动递增 - Auto-Increment using different databases 将准备好的语句与自动递增字段一起使用 - Using a prepared statement with auto increment field 使用 Java 生成自增数 - Generate auto increment number by using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM