我有3个对象:User,Comment和StatusUpdate(新闻)。这是用户......
@Entity
@Table(name = "users")
@PasswordMatch(message = "{register.repeatpassword.mismatch}")
public class SiteUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "email", unique = true)
@Email(message = "{register.email.invalid}")
@NotBlank(message = "{register.email.invalid}")
private String email;
@Transient
@Size(min = 5, max = 15, message = "{register.password.size}")
private String plainPassword;
@Column(name = "password", length = 60)
private String password;
@Column(name = "enabled")
private Boolean enabled = false;
@NotNull
@Column(name = "firstname", length = 20)
@Size(min = 2, max = 20, message = "{register.firstname.size}")
private String firstname;
@NotNull
@Column(name = "surname", length = 25)
@Size(min = 2, max = 25, message = "{register.surname.size}")
private String surname;
@Transient
private String repeatPassword;
@Column(name = "role", length = 20)
private String role;
public SiteUser() {
}
状态更新(您可以将其称为新闻或文章)。那个网站用户是创建该文章的用户。
@Entity
@Table(name = "status_update")
public class StatusUpdate {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min=5, max=255, message="{addstatus.title.size}")
@Column(name = "title")
private String title;
@Size(min=5, max=5000, message="{addstatus.text.size}")
@Column(name = "text")
private String text;
@Column(name = "added")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss")
private Date added;
@OneToOne(targetEntity = SiteUser.class)
@JoinColumn(name="user_id")
private SiteUser siteUser;
@PrePersist
protected void onCreate() {
if (added == null) {
added = new Date();
}
}
public StatusUpdate() {
}
可以由任何注册用户完成的评论,对吗?正如您将注意到,Comment没有User对象以避免循环引用。
@Entity
@Table(name = "comments")
public class Comment {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "statusupdateid")
private StatusUpdate statusUpdate;
@Column(name = "commenttext")
private String commenttext;
@Column(name = "commentdate")
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private Date commentdate;
@Column(name = "userid")
private Long userid;
public Comment() {
}
现在我想在我的JSP中显示一篇文章,其中包含所有相关注释,每个注释都属于不同的用户。我可以使用HashMap来关联用户及其评论吗?我不知道怎么做。
@RequestMapping(value ="/viewonestatus/{id}")
public ModelAndView viewOneStatus(@PathVariable("id") Long id) {
StatusUpdate status = statusUpdateService.get(id);
int countComments = commentService.countStatusComments(status);
List<Comment> comments = commentService.readAllComments(status);
ModelAndView modelAndView = new ModelAndView();
for (Comment comment: comments){
SiteUser user = userService.get(comment.getUserid());
modelAndView.getModel().put("user", user);
}
modelAndView.getModel().put("commentscounter", countComments);
modelAndView.getModel().put("status", status);
modelAndView.getModel().put("comments", comments); //!!
modelAndView.setViewName("app.viewonestatus");
return modelAndView;
}
如您所料,当我的JSP只显示所有注释的一个用户(最后一个)时,但我无法将所有注释与相应的用户关联
<table class="table table-hover">
<c:forEach var="comment" items="${comments}">
<tr>
<td>
<div class="col-sm-2 sm-margin-bottom-40">
<img class="img-responsive profile-img margin-bottom-20" id="profilePhotoImage" src="/profilephoto/${comment.userid}" />
</div>
<h4>
${user.firstname} ${user.surname}
<span>
<!-- <span>${counterUserMap[comment.key]}</span> -->
5 hours ago / <a href="#">Reply</a>
</span>
</h4>
<p>
<fmt:formatDate pattern="EEEE d MMMM y 'at' H:mm:ss" value="${comment.commentdate}" />
</p>
<p>${comment.commenttext}</p>
</td>
</tr>
</c:forEach>
我不想使用JSON。我正在考虑一个包含所有内容的匿名课程。好吧,我对你的想法持开放态度。感谢。
答案 0 :(得分:0)
Shokulei回答是解决方案:
由于您拥有userid,因此可以使用@ManyToOne批注对其进行链接。这将是最理想的方式。但如果你真的不想链接它们,那么你可以创建一个新的@Transient SiteUser siteUser; Comment类中的属性。然后在你的for循环中,你可以使用comment.setSiteUser(user);而不是modelAndView.getModel()。put(“user”,user);.希望这会有所帮助。
谢谢Shokulei