gpt4 book ai didi

java - 如何在 Spring 中发布用户列表

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:57 25 4
gpt4 key购买 nike

我在 Spring 中有这个域类:

@Entity
@Table(name="Like")
public class Like {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@ManyToOne(cascade= CascadeType.MERGE, targetEntity = User.class)
@JoinColumn(name = "user_id")
@OnDelete(action= OnDeleteAction.CASCADE)
Set<User> user;

@OneToMany(mappedBy = "like", orphanRemoval = true ,cascade= CascadeType.ALL, targetEntity = Picture.class)
@OnDelete(action= OnDeleteAction.CASCADE)
Set<Picture> pictures;

public Like() {
}

public Like(Set<User> user) {
this.user = user;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
@JsonIgnore
public Set<User> getUser() {
return user;
}
@JsonIgnore
public void setUser(Set<User> user) {
this.user = user;
}

@JsonIgnore
public Set<Picture> getPictures() {
return pictures;
}
@JsonIgnore
public void setPictures(Set<Picture> pictures) {
this.pictures = pictures;
}
}
}

我的 sql 脚本中有这个表

CREATE TABLE IF NOT EXISTS `like` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`user_id` BIGINT,
FOREIGN KEY (user_id) REFERENCES `user`(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

当我在 postman 中发送帖子请求时,这是一个问题:

java.lang.IllegalArgumentException:无法设置 java.lang.Long 字段 com.nyters.webapp.domain.User.id 到 java.util.HashSet

ControllerLike.java

  @RestController
@RequestMapping("api/like")
public class LikeController {

private LikeService likeService;

@Autowired
public LikeController(LikeService likeService){
this.likeService = likeService;
}

@RequestMapping(path = "/{id}", method = RequestMethod.GET)
public ResponseEntity<LikeDTO> findOne(@PathVariable Long id) {
LikeDTO pictureDTO = likeService.findOne(id);

if (pictureDTO != null) {
return new ResponseEntity<>(pictureDTO, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<LikeDTO>> findAll() {
List<LikeDTO> likeDTOs = likeService.findAll();

if (likeDTOs != null) {
return new ResponseEntity<>(likeDTOs, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<LikeDTO> save(@RequestBody String likeDtoString)
throws
IOException {
LikeDTO likeDTO = new ObjectMapper().readValue(likeDtoString,
LikeDTO.class);
LikeDTO saved = likeService.save(likeDTO);

if (saved != null) {
return new ResponseEntity<>(saved, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

最佳答案

我猜问题出在你的映射上

Like.java

@Fetch(value = FetchMode.SELECT)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "user_Id")
@JsonIgnore
private List<User> userList;
//based on user_Id u can fetch userList from DB

User.java

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_Id")
private Long user_Id;

关于java - 如何在 Spring 中发布用户列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43278324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com