6

我的数据库中有一个用 Identity(1,1) 设置的列,我无法让休眠注释为其工作。当我尝试创建新记录时出现错误。

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

@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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

我错过了什么?

4

6 回答 6

2

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

于 2010-05-25T14:50:05.617 回答
1

this combination works great for me:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
于 2010-05-25T14:47:43.923 回答
1

这是执行此操作的示例

@Id
@Column(name = "col_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long             colId;
于 2010-06-05T13:36:16.220 回答
0

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

于 2010-05-26T19:18:45.187 回答
0

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

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

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... } 
于 2010-05-25T14:43:58.813 回答
0

你不能手动创建表,一切都会好起来的。

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



}
于 2010-10-07T13:29:57.660 回答