如何使用JSON格式发布多个实体
我有两个实体如何使用JSON格式发布多个实体,json,spring-boot,jpa,Json,Spring Boot,Jpa,我有两个实体issue和user我想将用户分配给一个问题,但是当尝试使用这个JSON时,issues表中的用户id为NULL @Data @Entity @Table(name = "issues") public class Issue { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String number;
issue
和user
我想将用户分配给一个问题,但是当尝试使用这个JSON
时,issues
表中的用户id
为NULL
@Data
@Entity
@Table(name = "issues")
public class Issue {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String number;
@Column
private String title;
@Column
private String description;
@Column
@Enumerated(EnumType.STRING)
private State state;
@JsonIgnore
@CreationTimestamp
@Column
private Timestamp createDate;
@JsonIgnore
@UpdateTimestamp
@Column
private Timestamp modifyDate;
@ManyToOne(targetEntity = User.class)
@JoinColumn
private User user;
public Issue() {
}
首先,我创建了一个没有任何问题的用户,但不知道如何处理这个问题,这里是我通过postman使用的JSON
{
"number": "3",
"title": "Create an working service",
"description": "The problem is that we do not have an working service.",
"state": "NEW",
"user_id": "1"
}
此部件负责保存问题
public void save(Issue issue) {
if (issue == null)
return;
Issue actual = issueRepository.findByNumber(issue.getNumber());
if (actual != null) {
actual.setNumber(issue.getNumber());
actual.setTitle(issue.getTitle());
actual.setDescription(issue.getDescription());
actual.setState(issue.getState());
actual.setUser(issue.getUser());
issueRepository.save(actual);
} else {
issueRepository.save(issue);
}
}
在控制器中,我只有@Valid@RequestBody问题,服务保存了该问题 作为JSON发送的内容不是
问题
首先,因为它并不表示数据库持久性问题(这就是问题
类的目的),而是表示公共API期望从其客户端获得的JSON结构
第二个原因是问题
没有任何名为user\u id
的字段,但JSON有。因此,请使用与Issue
不同的类,该类实际上与API期望的JSON结构匹配,因此具有user\u id
属性
然后使用此user\u id
通过其id使用UserRepository
查找user
,并将该user
设置到您正在创建的问题中
我会将user\u id
重命名为userId
,以尊重Java约定。作为JSON发送的内容不是问题
首先,因为它并不表示数据库持久性问题(这就是问题
类的目的),而是表示公共API期望从其客户端获得的JSON结构
第二个原因是问题
没有任何名为user\u id
的字段,但JSON有。因此,请使用与Issue
不同的类,该类实际上与API期望的JSON结构匹配,因此具有user\u id
属性
然后使用此user\u id
通过其id使用UserRepository
查找user
,并将该user
设置到您正在创建的问题中
我会将user\u id
重命名为userId
,以遵守Java约定。您的Item类中没有显示user\u id密钥。
根据实体映射,
请将您的JSON更改为此
{
"number": "3",
"title": "Create an working service", "description": "The problem is that we do not have an working service.",
"state": "NEW",
"user":{
"fullName":"USER",
"email":"user@gmail.com",
"username":"user"
}
}
您的Item类中未显示用户id密钥。
根据实体映射,
请将您的JSON更改为此
{
"number": "3",
"title": "Create an working service", "description": "The problem is that we do not have an working service.",
"state": "NEW",
"user":{
"fullName":"USER",
"email":"user@gmail.com",
"username":"user"
}
}
您需要像这样发布json
{
"number": "3",
"title": "Create an working service",
"description": "The problem is that we do not have an working service.",
"state": "NEW",
"user":{
"id":1
}
}
public void save(Issue issue) {
if (issue == null)
return;
Issue actual = issueRepository.findByNumber(issue.getNumber());
if (actual != null) {
actual.setNumber(issue.getNumber());
actual.setTitle(issue.getTitle());
actual.setDescription(issue.getDescription());
actual.setState(issue.getState());
actual.setUser(userRepositoy.findById(issue.getUser().getId()));
issueRepository.save(actual);
} else {
issueRepository.save(issue);
}
}
这里有一个用户的join列,所以对于用户实体,您必须传递userid。
你的储蓄是这样的
{
"number": "3",
"title": "Create an working service",
"description": "The problem is that we do not have an working service.",
"state": "NEW",
"user":{
"id":1
}
}
public void save(Issue issue) {
if (issue == null)
return;
Issue actual = issueRepository.findByNumber(issue.getNumber());
if (actual != null) {
actual.setNumber(issue.getNumber());
actual.setTitle(issue.getTitle());
actual.setDescription(issue.getDescription());
actual.setState(issue.getState());
actual.setUser(userRepositoy.findById(issue.getUser().getId()));
issueRepository.save(actual);
} else {
issueRepository.save(issue);
}
}
您需要像这样发布json
{
"number": "3",
"title": "Create an working service",
"description": "The problem is that we do not have an working service.",
"state": "NEW",
"user":{
"id":1
}
}
public void save(Issue issue) {
if (issue == null)
return;
Issue actual = issueRepository.findByNumber(issue.getNumber());
if (actual != null) {
actual.setNumber(issue.getNumber());
actual.setTitle(issue.getTitle());
actual.setDescription(issue.getDescription());
actual.setState(issue.getState());
actual.setUser(userRepositoy.findById(issue.getUser().getId()));
issueRepository.save(actual);
} else {
issueRepository.save(issue);
}
}
这里有一个用户的join列,所以对于用户实体,您必须传递userid。
你的储蓄是这样的
{
"number": "3",
"title": "Create an working service",
"description": "The problem is that we do not have an working service.",
"state": "NEW",
"user":{
"id":1
}
}
public void save(Issue issue) {
if (issue == null)
return;
Issue actual = issueRepository.findByNumber(issue.getNumber());
if (actual != null) {
actual.setNumber(issue.getNumber());
actual.setTitle(issue.getTitle());
actual.setDescription(issue.getDescription());
actual.setState(issue.getState());
actual.setUser(userRepositoy.findById(issue.getUser().getId()));
issueRepository.save(actual);
} else {
issueRepository.save(issue);
}
}
你要把它送到什么地方?接收此JSON的代码如何解析它?一旦被解析,它会做什么?所有这些代码都很重要。你要把它们发送给什么?接收此JSON的代码如何解析它?一旦被解析,它会做什么?所有这些代码都很重要。如果不是问题,你能给我一个简单的例子吗?如果我的解释中有不清楚的地方,请告诉我并要求澄清。如果一切都可以理解,那么至少花几分钟亲自尝试一下。这是一个人学习的方式。但是你有什么解决办法吗?除了你上面提供的“简易非标准”解决方案之外?如果不是问题,你能给我一个简单的例子吗?如果我的解释不清楚,请告诉我并要求澄清。如果一切都可以理解,那么至少花几分钟亲自尝试一下。这是一个人学习的方式。但是你有什么解决办法吗?除了上面提供的“简易非标准”解决方案之外?但这将插入一个新用户!他想将新问题链接到现有用户。@如果他想链接到现有用户,那么他需要在用户JSON中添加id
属性aka主键。但这将插入一个新用户!他想将新问题链接到现有用户。@Mehdi如果他想链接到现有用户,那么他需要在用户JSON中添加id
属性aka主键。