Jersey JAX-RS自动生成的web服务和MSSQL标识字段



我无法使用Jersey rest式web服务,它是由Netbeans自动生成的。

当像这样发布JSON时,

{"name":"Some New Site"}

POST查询返回给我这个

Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert
explicit value for identity column in table 'site' when IDENTITY_INSERT is set to OFF.
Error Code: 544
Call: INSERT INTO site (id, name) VALUES (?, ?)
    bind => [2 parameters bound]

不像MySQL, MSSQL不会在INSERT查询中传入NULL值时生成新的自增ID;它需要ID字段来为它生成值。

(IDENTITY_INSERT也不会生成ID)

我如何教我的Jersey服务在INSERT查询中省略id字段?

edit:这是POJO,它是由Netbeans与它的"Restful Web Services From Database"向导自动为我生成的。(我尽量不那么原创)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.fedd.entities;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
 *
 * @author fkravchenko
 */
@Entity
@Table(name = "site")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Site.findAll", query = "SELECT s FROM Site s"),
    @NamedQuery(name = "Site.findById", query = "SELECT s FROM Site s WHERE s.id = :id"),
    @NamedQuery(name = "Site.findByName", query = "SELECT s FROM Site s WHERE s.name = :name")})
public class Site implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "siteId")
    private Collection<Document> documentCollection;
    public Site() {
    }
    public Site(Long id) {
        this.id = id;
    }
    public Site(Long id, String name) {
        this.id = id;
        this.name = name;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlTransient
    public Collection<Document> getDocumentCollection() {
        return documentCollection;
    }
    public void setDocumentCollection(Collection<Document> documentCollection) {
        this.documentCollection = documentCollection;
    }
    @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 Site)) {
            return false;
        }
        Site other = (Site) 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 "ru.fedd.entities.Site[ id=" + id + " ]";
    }
}

你能在这里分享你的POJO类吗?

您可能必须用@GeneratedValue注释POJO类的id,以通知它是自动生成的。然后JPA将负责插入。

相关内容

  • 没有找到相关文章

最新更新