Hibernate 自动增量不起作用

发布于 2024-09-02 17:09:10 字数 2047 浏览 6 评论 0原文

我的数据库中有一个列设置为 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”中插入标识列的显式值。我不想将 IDENTIY_INSERT 设置为 ON,因为我希望数据库中的标识列来管理值。

运行的 SQL 如下;您可以清楚地看到插入物的位置。

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

我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

趁年轻赶紧闹 2024-09-09 17:09:10

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

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.

燕归巢 2024-09-09 17:09:10

这个组合对我来说非常有用:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

this combination works great for me:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
彻夜缠绵 2024-09-09 17:09:10

这是执行此操作的示例

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

Here is the example to do it

@Id
@Column(name = "col_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long             colId;
橘虞初梦 2024-09-09 17:09:10

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

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

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

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() { ... } 
悲欢浪云 2024-09-09 17:09:10

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

You can't use Generators on composite keys

剧终人散尽 2024-09-09 17:09:10

你不能这样做
手动创建表,一切都会好的。

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



}

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



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文