我在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;
当我在邮递员发送邮件请求时,这是一个问题:
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);
}
}
答案 0 :(得分:0)
首先@JsonIgnore
使您的用户无法从json访问实体。
第二步,您需要将已提交@ManyToOne
的{{1}}更改为@OneToMany
或@ManyToMany
,或将类型更改为Set<User> user
<强>更新强>
嗯,User user
意味着在当前班级中你只有一个元素(在我们的例子中是ManyToOne
。它看起来如下:
User
在这种情况下,您的用户类如下所示:
class Like {
// init, class and bla-bla-bla
@ManyToOne(/*properties*/)
User user; // important - not Collection, Set or something else
所以,你可以想象(?)如下:@external_filed到current_field。我希望你理解
答案 1 :(得分:0)
我猜概率是你的映射
<强> 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;