Hibernate Auto-Increment not working
Asked Answered
B

6

6

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?

Biggers answered 25/5, 2010 at 14:36 Comment(4)
I've updated the post to include the entire file without getters/setters.Biggers
I think, we should make Hibernate not to insert SeqNo. Possible marking of SeqNo getter as @Id and not using generator will help.Phytoplankton
I've tried setting insertable=false and updateble=false, but that didn't help.Biggers
Furthermore, i've got a composite key, so i can't set it as @IdBiggers
U
2

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.

Under answered 25/5, 2010 at 14:50 Comment(1)
This was my original setup, and it produced the same error. I got the "native" suggestion from another SOF post.Biggers
B
1

this combination works great for me:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Bybee answered 25/5, 2010 at 14:47 Comment(0)
P
1

Here is the example to do it

@Id
@Column(name = "col_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long             colId;
Parboil answered 5/6, 2010 at 13:36 Comment(0)
P
0

Possible you need to mark your field with @id and not specify generator property.

As showed in Hibernate Annotation - 2.2.3.1. Generating the identifier property, the next example uses the identity generator:

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... } 
Phytoplankton answered 25/5, 2010 at 14:43 Comment(1)
My ID is composite, I've updated the post with the whole fileBiggers
B
0

You can't use Generators on composite keys

Biggers answered 26/5, 2010 at 19:18 Comment(1)
same problem with me.Kick
A
0

You can't do it with Create table manually and everything will be ok.

CREATE TABLE `Forum` (
  `name` varchar(255) NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `body` varchar(500) DEFAULT NULL,
  PRIMARY KEY (name,`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin2




import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

@Entity
public class Forum implements Serializable {

    @EmbeddedId
    private ForumCompositePK forumPK;
    /**
     * 
     */
    private static final long serialVersionUID = 7070007885798411858L;

    @Column(length = 500)
    String body;

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void setForumPK(ForumCompositePK forumPK) {
        this.forumPK = forumPK;
    }

    public  ForumCompositePK getForumPK() {
        return forumPK;
    }

}




import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

@Embeddable
public class ForumCompositePK implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8277531190469885913L;


    @Column(unique=true,updatable=false,insertable=false)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   

    public Integer getId() {
        return id;
    }   

    public void setId(Integer id) {
        this.id = id;
    }



}
Alburg answered 7/10, 2010 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.