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

时间:2015-10-24 16:44:57

标签: java jpa orm

我有一个带有简单int id列的表,在SQL Server中具有Identity auto increment。

    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',table' master.dbo.InstagramRequest&#39 ;;列不允许空值。 INSERT失败。 错误代码:515 调用:INSERT INTO InstagramRequest(instUserID,intime,request)VALUES(?,?,?)     bind => [3个参数绑定] 查询:InsertObjectQuery(com.instagramparse.entity.InstagramRequest [id = null])

在这种情况下我应该使用哪种GenerationType?

1 个答案:

答案 0 :(得分:1)

您的GenerationType是正确的。您需要删除@Basic(optional = false) - 如果要由数据库自动生成,则强制此字段由JPA设置是没有意义的。

事实上,似乎发生的事情是您的JPA提供程序尝试插入NULL值而不是为id列设置任何内容。由于列是自动生成的,因此INSERT中的该列中不能插入任何值。使字段可选将按预期工作 - JPA不会尝试将任何值插入到数据库中,但会在插入后读取生成的值,使值在持久化后始终为非空。