[英]JPA GenerationType.IDENTITY not considering column with auto increment in MS SQL
I have a table with a simple int id column with Identity auto increment in SQL Server.我有一个表,其中包含一个简单的 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
Entity class is ;实体类是;
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 + " ]";
}
}
The error message is as below .;错误信息如下。
Internal Exception: java.sql.SQLException: Cannot insert the value NULL into column 'ID', table 'master.dbo.InstagramRequest';内部异常:java.sql.SQLException:无法将值 NULL 插入列“ID”、表“master.dbo.InstagramRequest”; column does not allow nulls.
列不允许空值。 INSERT fails.
插入失败。 Error Code: 515 Call: INSERT INTO InstagramRequest (instUserID, intime, request) VALUES (?, ?, ?) bind => [3 parameters bound] Query: InsertObjectQuery(com.instagramparse.entity.InstagramRequest[ id=null ])
错误代码:515 调用:INSERT INTO InstagramRequest (instUserID, intime, request) VALUES (?, ?, ?) bind => [3 个参数绑定] 查询:InsertObjectQuery(com.instagramparse.entity.InstagramRequest[ id=null ])
Which GenerationType should I use in this case ?在这种情况下我应该使用哪个 GenerationType?
Your GenerationType
is correct.您的
GenerationType
是正确的。 You need to remove @Basic(optional = false)
- it does not make sense to enforce that this field is set by JPA if it is to be autogenerated by the DB.您需要删除
@Basic(optional = false)
- 如果要由 DB 自动生成,则强制此字段由 JPA 设置是没有意义的。
In fact, what seems to happen is that your JPA provider tries to insert NULL value instead of not setting anything for the id column.事实上,似乎发生的事情是您的 JPA 提供程序尝试插入 NULL 值,而不是不为 id 列设置任何内容。 As the column is autogenerated, no value can be inserted into that column in an
INSERT
.由于该列是自动生成的,因此不能在
INSERT
向该列中插入任何值。 Making the field optional will work as expected - JPA will not try to insert any value for I'd into the db, but will read the generated value after insert, making the value to always be non-null after persist.使字段可选将按预期工作 - JPA 不会尝试将任何值插入到数据库中,但会在插入后读取生成的值,使值在持久化后始终为非空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.