我是春季靴子的新手。我有一個 Comment 類,它與 User 類有一對多的關系。現在我想創建一個帶有發布請求的評論。但是我怎樣才能只傳遞寫評論的用戶的 userId 呢?所以我可以將這個物件保存到資料庫中。
我希望有人可以幫助我。我會很感激。
發布請求:
{
"message": "This is a message",
"writer_id": 1
}
用戶
@Entity
@Table(name="tbl_users")
public class User {
@Id
@GeneratedValue(
strategy = GenerationType.AUTO,
generator = "user_sequence"
)
@SequenceGenerator(
name = "user_sequence",
allocationSize = 1,
sequenceName = "user_sequence"
)
private Long id;
private String name;
public User(String name) {
this.name = name;
}
public User(Long id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
getter, setter ...
評論
@Entity
@Table(name="tbl_comments")
public class Comment {
@Id
@GeneratedValue(
strategy = GenerationType.AUTO,
generator = "comment_sequence"
)
@SequenceGenerator(
name = "comment_sequence",
allocationSize = 1,
sequenceName = "comment_sequence"
)
private Long id;
private String message;
@ManyToOne
@JoinColumn(name="user_id", nullable=false)
private User writer;
public Comment(String message, User writer) {
this.message = message;
this.writer = writer;
}
public Comment(Long id, String message, User writer) {
this.id = id;
this.message = message;
this.writer = writer;
}
public Comment() {
}
getter, setter...
評論控制器
@PostMapping
public void createComment(@RequestBody Comment comment) {
commentService.createComment(comment);
}
評論服務
public void createComment(Comment comment) {
if(!userRepository.existsById(comment.getWriter().getId())) {
throw new UserNotFoundException("Writer does not exist");
}
commentRepository.save(comment);
}
uj5u.com熱心網友回復:
以下是一些要遵循的步驟: 我在這里所做的是基本實作,因為我沒有考慮用戶是否經過身份驗證。
1- 添加以下關系到用戶物體
public class User {
/**
other attributes
**/
//relation with comment
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY, orphanRemoval = true,
mappedBy = "user")
private Set<Comment> comments;
}
2-然后您還應該將關系添加到 Comment 物體:
public class Comment {
/**
other attributes
**/
//relation with user
@ManyToOne()
@JoinColumn(name = "user_id")
private User user;
}
3- 在評論服務中,只需將 userId 作為引數添加到您的函式中
public class CommentService {
private CommentRepository commentRepository;
private UserRepository userRepository;
public Comment userPostComment(Comment comment, Long userId){
User user = userRepository.findById(userId).orElseThrow(
() -> new UsernameNotFoundException("User not found"));
/**
set other attributes
**/
//add found user to the comment
comment.setUser(user);
return commentRepository.save(comment);
}
4- 最后在你的控制器,你應該給 userId 作為 PathVariable
public class CommentController {
private CommentService commentService;
@PostMapping("/addComment/{userId}")
public ResponseEntity<Comment> addComment(@RequestBody Comment comment,
@PathVariable Long userId){
return new ResponseEntity<>(
commentService.userPostComment(comment, userId),
HttpStatus.CREATED);
}
}
uj5u.com熱心網友回復:
解決方案之一是創建 CommentRequest 類
public class CommentRequest {
private String message;
private int userId;
getters, setters
}
然后在 CommentController 中,您應該將方法更改為:
@PostMapping
public void createComment(@RequestBody CommentRequest commentRequest) {
commentService.createComment(commentRequest);
}
并且在 CommentService 中:
public void createComment(CommentRequest commentRequest) {
if(!userRepository.existsById(commentRequest.getUserId())) {
throw new UserNotFoundException("Writer does not exist");
}
User user = userRepository.getById(commentRequest.getUserId());
commentRepository.save(new Comment(commentRequest.getMessage(), user));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/536409.html
上一篇:無法使用@mapper映射列舉值
