休眠自动增量无法正常工作

时间:2010-05-25 14:36:26

标签: java database hibernate orm composite-key

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

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

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

我错过了什么?

6 个答案:

答案 0 :(得分:1)

这种组合对我很有用:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

答案 1 :(得分:1)

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

答案 2 :(得分:1)

以下是执行此操作的示例

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

答案 3 :(得分:0)

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

Hibernate Annotation - 2.2.3.1. Generating the identifier property所示,下一个示例使用身份生成器:

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

答案 4 :(得分:0)

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

答案 5 :(得分: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;
    }



}