java 休眠自动增量不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2905645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:22:18  来源:igfitidea点击:

Hibernate Auto-Increment not working

javadatabasehibernateormcomposite-key

提问by dharga

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.

我的数据库中有一个列设置了 Identity(1,1) 并且我无法获得休眠注释来为它工作。尝试创建新记录时出现错误。

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.

当 IDENTITY_INSERT 设置为 OFF 时,无法为表“MemberSelectedOptions”中的标识列插入显式值。我不想将 IDENTIY_INSERT 设置为 ON,因为我希望数据库中的标识列管理这些值。

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

运行的 SQL 如下;在那里您可以清楚地看到插入物。

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

What am I missing?

我错过了什么?

回答by Andrey

this combination works great for me:

这种组合对我很有用:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

回答by Pascal Thivent

When you use @Embeddableor @EmbeddedId, the primary key values are supposed to be providedby the application (i.e. made up of non generated values). Your @GeneratedValueannotation is just ignored.

当您使用@Embeddableor 时@EmbeddedId,主键值应该由应用程序提供(即由非生成值组成)。您的@GeneratedValue注释只是被忽略。

回答by Rupeshit

Here is the example to do it

这是执行此操作的示例

@Id
@Column(name = "col_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long             colId;

回答by baklarz2048

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;
    }



}

回答by dharga

You can't use Generators on composite keys

您不能在复合键上使用生成器

回答by Serge S.

Possible you need to mark your field with @idand not specify generatorproperty.

您可能需要使用@id而不是指定generator属性来标记您的字段。

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

Hibernate Annotation - 2.2.3.1 所示。生成标识符属性,下一个示例使用身份生成器:

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... }