Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Auto-Increment not working

I have a column in my DB that is set with Identity(1,1) and I can't get hibernate annotations to work for it. I get errors when I try to create a new record.

In my entity I have the following.

@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...
}

But when I try to add a new record I get the following error.

Cannot insert explicit value for identity column in table 'MemberSelectedOptions' when IDENTITY_INSERT is set to OFF. I don't want to set IDENTIY_INSERT to ON because I want the identity column in the db to manage the values.

The SQL that is run is the following; where you can clearly see the insert.

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

What am I missing?

like image 246
dharga Avatar asked May 25 '10 14:05

dharga


People also ask

How to make auto increment in Hibernate?

If you want to use this strategy, you have to annotate the primary key attribute @Id and with the @GeneratedValue annotation and set the strategy attribute to GenerationType. IDENTITY. If you now persist a new Author entity, Hibernate will use the auto-incremented database column to generate the primary key value.

Does auto increment have to be primary key?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

What is @GeneratedValue strategy GenerationType Auto?

GenerationType. AUTO This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.


1 Answers

When you use @Embeddable or @EmbeddedId, the primary key values are supposed to be provided by the application (i.e. made up of non generated values). Your @GeneratedValue annotation is just ignored.

like image 73
Pascal Thivent Avatar answered Oct 03 '22 18:10

Pascal Thivent