编程语言
首页 > 编程语言> > java – Hibernate自动增量不起作用

java – Hibernate自动增量不起作用

作者:互联网

我的数据库中有一个用Identity(1,1)设置的列,我无法获得hibernate注释.我尝试创建新记录时遇到错误.

在我的实体中,我有以下内容.

@Entity
@Table(schema="dbo", name="MemberSelectedOptions")
public class MemberSelectedOption extends BampiEntity implements Serializable {

    @Embeddable
    public static class MSOPK implements Serializable {
        private static final long serialVersionUID = 1L;

        @Column(name="SourceApplication")
        String sourceApplication;

        @Column(name="GroupId")
        String groupId;

        @Column(name="MemberId")
        String memberId;

        @Column(name="OptionId")
        int optionId;

        @GeneratedValue(strategy=GenerationType.IDENTITY, generator="native")
        @Column(name="SeqNo", unique=true, nullable=false)
        BigDecimal seqNo;

        //Getters and setters here...

    }

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    MSOPK pk = new MSOPK();

    @Column(name="OptionStatusCd")
    String optionStatusCd;

    @Column(name="EffectiveDate")
    Date effectiveDate;

    @Column(name="TermDate")
    Date termDate;

    @Column(name="SelectionStatusDate")
    Date selectionStatusDate;   

    @Column(name="SysLstUpdtUserId")
    String sysLstUpdtUserId = Globals.WS_USER_ID;;

    @Column(name="SysLstTrxDtm")
    Date sysLstTrxDtm = new Date();

    @OneToMany(mappedBy="option")
    List<MemberSelectedVariable> variables = 
                             new ArrayList<MemberSelectedVariable>();

        //More Getters and setters here...
}

但是当我尝试添加新记录时,我收到以下错误.

当IDENTITY_INSERT设置为OFF时,无法在表’MemberSelectedOptions’中为identity列插入显式值.我不想将IDENTIY_INSERT设置为ON,因为我希望db中的identity列管理值.

运行的SQL如下;在哪里可以清楚地看到插入物.

insert into dbo.MemberSelectedOptions 
  (OptionStatusCd, 
  EffectiveDate,
  TermDate, 
  SelectionStatusDate, 
  SysLstUpdtUserId, 
  SysLstTrxDtm, 
  SourceApplication,
  GroupId,
  MemberId, 
  OptionId, 
  SeqNo) 
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

我错过了什么?

解决方法:

这个组合对我很有用:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

标签:java,database,orm,hibernate,composite-key
来源: https://codeday.me/bug/20190701/1342653.html