Jersey JAX-RS auto-generated webservices and MSSQL Identity fields

By : fedd

I can't get working the Jersey RESTful webservices, autogenerated with Netbeans.

When POSTing the JSON like this,

{"name":"Some New Site"}

the POST query returns me this,

Internal Exception: 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]

Unlike MySQL, MSSQL doesn't generate new autoincremented ID when the NULL value is passed in INSERT query; it needs the ID field not to mentioned at all to generate value for it.

(IDENTITY_INSERT will not make ID to be generated either)

How do I teach my Jersey services to omit the id field in INSERT query?

edit: here's the POJO, it was generated automatically for me by Netbeans with it's "Restful Web Services From Database" wizard. (I am trying to be as little original as possible)

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package ru.fedd.entities;

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
@Table(name = "site")
    @NamedQuery(name = "Site.findAll", query = "SELECT s FROM Site s"),
    @NamedQuery(name = "Site.findById", query = "SELECT s FROM Site s WHERE = :id"),
    @NamedQuery(name = "Site.findByName", query = "SELECT s FROM Site s WHERE = :name")})
public class Site implements Serializable {
    private static final long serialVersionUID = 1L;
    @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) { = id;

    public Site(Long id, String name) { = id; = name;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Collection<Document> getDocumentCollection() {
        return documentCollection;

    public void setDocumentCollection(Collection<Document> documentCollection) {
        this.documentCollection = documentCollection;

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

    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 (( == null && != null) || ( != null && ! {
            return false;
        return true;

    public String toString() {
        return "ru.fedd.entities.Site[ id=" + id + " ]";

By : fedd

This video can help you solving your question :)
By: admin