Jersey JAX-RS 自动生成的 Web 服务和 MSSQL 身份字段

发布于 2024-12-23 00:19:32 字数 3479 浏览 0 评论 0原文

我无法使用由 Netbeans 自动生成的 Jersey RESTful Web 服务。

当像这样发布 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 不同,当在 INSERT 查询中传递 NULL 值时,MSSQL 不会生成新的自动增量 ID;它需要根本没有提及的 ID 字段来为其生成值。

(IDENTITY_INSERT 也不会生成 ID)

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

编辑: 这是 POJO,它是生成的Netbeans 通过“来自数据库的 Restful Web 服务”向导自动为我服务。 (我试图尽可能少地原创)

/*
 * 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 + " ]";
    }

}

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

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.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 + " ]";
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2024-12-30 00:19:32

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

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

Can you share your POJO class here?

You probably have to annotate your id of the POJO class with @GeneratedValue to notify it is auto generated. Then JPA will take care of the insertion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文