簡體   English   中英

JPA GenerationType.IDENTITY 不考慮 MS SQL 中具有自動增量的列

[英]JPA GenerationType.IDENTITY not considering column with auto increment in MS SQL

我有一個表,其中包含一個簡單的 int id 列,在 SQL Server 中具有標識自動增量。

    USE [Hot]
GO

/****** Object:  Table [dbo].[InstagramRequest]    Script Date: 24.10.2015 18:49:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[InstagramRequest](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [instUserId] [int] NULL,
    [request] [nvarchar](max) NULL,
    [intime] [datetime] NULL,
 CONSTRAINT [PK_InstagramRequest] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 10) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[InstagramRequest] ADD  CONSTRAINT [DF_InstagramRequest_intime]  DEFAULT (getdate()) FOR [intime]
GO

實體類是;

    import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author z
 */
@Entity
@Table(name = "InstagramRequest")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "InstagramRequest.findAll", query = "SELECT i FROM InstagramRequest i"),
    @NamedQuery(name = "InstagramRequest.findById", query = "SELECT i FROM InstagramRequest i WHERE i.id = :id"),
    @NamedQuery(name = "InstagramRequest.findByInstUserID", query = "SELECT i FROM InstagramRequest i WHERE i.instUserID = :instUserID"),
    @NamedQuery(name = "InstagramRequest.findByIntime", query = "SELECT i FROM InstagramRequest i WHERE i.intime = :intime")})
public class InstagramRequest implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "instUserID")
    private Integer instUserID;
    @Lob
    @Column(name = "request")
    private String request;
    @Column(name = "intime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date intime;

    public InstagramRequest() {
    }

    public InstagramRequest(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getInstUserID() {
        return instUserID;
    }

    public void setInstUserID(Integer instUserID) {
        this.instUserID = instUserID;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public Date getIntime() {
        return intime;
    }

    public void setIntime(Date intime) {
        this.intime = intime;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof InstagramRequest)) {
            return false;
        }
        InstagramRequest other = (InstagramRequest) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.instagramparse.entity.InstagramRequest[ id=" + id + " ]";
    }

}

錯誤信息如下。

內部異常:java.sql.SQLException:無法將值 NULL 插入列“ID”、表“master.dbo.InstagramRequest”; 列不允許空值。 插入失敗。 錯誤代碼:515 調用:INSERT INTO InstagramRequest (instUserID, intime, request) VALUES (?, ?, ?) bind => [3 個參數綁定] 查詢:InsertObjectQuery(com.instagramparse.entity.InstagramRequest[ id=null ])

在這種情況下我應該使用哪個 GenerationType?

您的GenerationType是正確的。 您需要刪除@Basic(optional = false) - 如果要由 DB 自動生成,則強制此字段由 JPA 設置是沒有意義的。

事實上,似乎發生的事情是您的 JPA 提供程序嘗試插入 NULL 值,而不是不為 id 列設置任何內容。 由於該列是自動生成的,因此不能在INSERT向該列中插入任何值。 使字段可選將按預期工作 - JPA 不會嘗試將任何值插入到數據庫中,但會在插入后讀取生成的值,使值在持久化后始終為非空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM