Java Spring数据驻留了许多POST

Java Spring数据驻留了许多POST,java,spring,spring-data-jpa,spring-data-rest,Java,Spring,Spring Data Jpa,Spring Data Rest,首先,让我解释一下我的用例。这很直截了当。有一个用户实体和一个服务实体。我使用UserService作为联接实体(联接表),在用户和服务之间有许多关联。最初,将有一些用户集和一些服务集。用户可以在任何时间点订阅任何服务。在这种情况下,将向UserService添加一个条目。但是,当我试图创建一个新的UserService关联时,我得到了空指针异常。我可以单独创建用户和服务 我的实体是: User.java package dao.models; import java.io.Serializa

首先,让我解释一下我的用例。这很直截了当。有一个用户实体和一个服务实体。我使用UserService作为联接实体(联接表),在用户和服务之间有许多关联。最初,将有一些用户集和一些服务集。用户可以在任何时间点订阅任何服务。在这种情况下,将向UserService添加一个条目。但是,当我试图创建一个新的UserService关联时,我得到了空指针异常。我可以单独创建用户和服务

我的实体是: User.java

package dao.models;

import java.io.Serializable;
import javax.persistence.*;

import com.fasterxml.jackson.annotation.JsonBackReference;
@Entity
@org.hibernate.annotations.Proxy(lazy=false)
@Table(name="`user`", schema="emm")
public class User implements Serializable {
    public User() {
    }

    @Column(name="id", nullable=false, unique=true) 
    @Id 
    @GeneratedValue(generator="EMM_USER_ID_GENERATOR")  
    @org.hibernate.annotations.GenericGenerator(name="EMM_USER_ID_GENERATOR", strategy="native")    
    private long id;


    @ManyToOne(targetEntity=dao.models.Tenant.class, fetch=FetchType.LAZY)  
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})    
    @JoinColumns({ @JoinColumn(name="tenant_id", referencedColumnName="id", nullable=false) })  
    @org.hibernate.annotations.LazyToOne(value=org.hibernate.annotations.LazyToOneOption.NO_PROXY)  
    private dao.models.Tenant tenant;

    @OneToOne(targetEntity=dao.models.Person.class, fetch=FetchType.LAZY)   
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
    @JoinColumns({ @JoinColumn(name="Person_id", nullable=false) }) 
    @org.hibernate.annotations.LazyToOne(value=org.hibernate.annotations.LazyToOneOption.NO_PROXY)  
    private dao.models.Person person;

    @Column(name="password", nullable=true, length=255) 
    private String password;

    @Column(name="email", nullable=false, length=255)   
    private String email;

    @Column(name="status", nullable=true, length=255)   
    private String status;

    @ManyToMany(mappedBy="user", targetEntity=dao.models.TenantGroup.class) 
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
    @org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)  
    private java.util.List<dao.models.TenantGroup> group = new java.util.ArrayList<dao.models.TenantGroup>();

    @OneToMany(mappedBy="user", targetEntity=dao.models.UserService.class)  
    @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
    @org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)  
    private java.util.List<dao.models.UserService> userService = new java.util.ArrayList<dao.models.UserService>();

    public void setId(long value) {
        this.id = value;
    }

    public long getId() {
        return id;
    }



    public void setPassword(String value) {
        this.password = value;
    }

    public String getPassword() {
        return password;
    }

    public void setEmail(String value) {
        this.email = value;
    }

    public String getEmail() {
        return email;
    }

    public void setStatus(String value) {
        this.status = value;
    }

    public String getStatus() {
        return status;
    }

    public void setTenant(dao.models.Tenant value) {
        this.tenant = value;
    }

    public dao.models.Tenant getTenant() {
        return tenant;
    }

    public void setPerson(dao.models.Person value) {
        this.person = value;
    }

    public dao.models.Person getPerson() {
        return person;
    }

    public void setGroup(java.util.List<dao.models.TenantGroup> value) {
        this.group = value;
    }

    public java.util.List<dao.models.TenantGroup> getGroup() {
        return group;
    }




    public java.util.List<dao.models.UserService> getUserService() {
        return userService;
    }

    public void setUserService(
            java.util.List<dao.models.UserService> userService) {
        this.userService = userService;
    }

    public String toString() {
        return String.valueOf(getId());
    }

}
错误消息:

{
    "cause": {
        "cause": {
            "cause": null,
            "message": null
        },
        "message": "(was java.lang.NullPointerException) (through reference chain: dao.models.UserService[\"service\"])"
    },
    "message": "Could not read JSON: (was java.lang.NullPointerException) (through reference chain: dao.models.UserService[\"service\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: dao.models.UserService[\"service\"])"
}
获取
http://localhost:8080/em/api/userServices
为我提供以下输出:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/em/api/userServices{?page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "userServices" : [ {
      "paramName" : "p1",
      "paramValue" : "v1",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/em/api/userServices/1"
        },
        "userServiceToken" : {
          "href" : "http://localhost:8080/em/api/userServices/1/userServiceToken"
        },
        "user" : {
          "href" : "http://localhost:8080/em/api/userServices/1/user"
        },
        "service" : {
          "href" : "http://localhost:8080/em/api/userServices/1/service"
        }
      }
    }, {
      "paramName" : "pone",
      "paramValue" : "vone",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/em/api/userServices/2"
        },
        "userServiceToken" : {
          "href" : "http://localhost:8080/em/api/userServices/2/userServiceToken"
        },
        "user" : {
          "href" : "http://localhost:8080/em/api/userServices/2/user"
        },
        "service" : {
          "href" : "http://localhost:8080/em/api/userServices/2/service"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

是否有人使用SpringDataREST成功实现了多个关联。如果是这样的话,请在这方面帮助我

我解决了这个问题并使它工作起来

以前,我的请求主体是:

{
    "paramName": "p1",
    "paramValue": "v1",
    "service": {
        "href": "http://localhost:8080/em/api/userServices/1/service/2"
    },
    "user": {
        "href": "http://localhost:8080/em/api/userServices/1/user/1"
    }
}
我想应该是这样的:

{
    "paramName": "p1",
    "paramValue": "v1",
    "service":  "http://localhost:8080/em/api/services/2",
    "user":  "http://localhost:8080/em/api/users/1"
 }
我觉得spring数据rest仍然存在问题。如果有人不这么认为,请澄清。即使有固定的请求,我也得到ServiceId的null约束。我发现在db中,服务的主键列是service_id。尽管我正确地进行了实体映射(service entity中的id属性正确地映射到db中的service_id),但它不起作用,我必须将列名更改为id才能使其起作用

Spring数据Rest应该取决于Id的实体映射,对吗?如果是这样,那么仍然存在一个bug

谢谢,
Vivek

在网上,我看到了GET的例子,但没有发布或放置具有关联性的资源的例子您关于Id的问题应该作为单独的问题发布。您的服务id有@id注释吗?如果不是,那可能是你的问题。
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/em/api/userServices{?page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "userServices" : [ {
      "paramName" : "p1",
      "paramValue" : "v1",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/em/api/userServices/1"
        },
        "userServiceToken" : {
          "href" : "http://localhost:8080/em/api/userServices/1/userServiceToken"
        },
        "user" : {
          "href" : "http://localhost:8080/em/api/userServices/1/user"
        },
        "service" : {
          "href" : "http://localhost:8080/em/api/userServices/1/service"
        }
      }
    }, {
      "paramName" : "pone",
      "paramValue" : "vone",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/em/api/userServices/2"
        },
        "userServiceToken" : {
          "href" : "http://localhost:8080/em/api/userServices/2/userServiceToken"
        },
        "user" : {
          "href" : "http://localhost:8080/em/api/userServices/2/user"
        },
        "service" : {
          "href" : "http://localhost:8080/em/api/userServices/2/service"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}
{
    "paramName": "p1",
    "paramValue": "v1",
    "service": {
        "href": "http://localhost:8080/em/api/userServices/1/service/2"
    },
    "user": {
        "href": "http://localhost:8080/em/api/userServices/1/user/1"
    }
}
{
    "paramName": "p1",
    "paramValue": "v1",
    "service":  "http://localhost:8080/em/api/services/2",
    "user":  "http://localhost:8080/em/api/users/1"
 }