我一直在开发简单的社交网络项目,我要以AppUser.java的形式实现User -> Friend
关系:
@Data
@Entity(name = "users")
@NoArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class AppUser extends AuditModel {
@NonNull
private String userName;
@NonNull
@Column(unique = true, nullable = false)
private String userEmail;
private String userPassword;
@OneToMany( mappedBy="owner", cascade=CascadeType.ALL, orphanRemoval = true)
private List<Friend> friends = new ArrayList<>();
}
和Friend.java
@Data
@Entity(name = "friends")
@NoArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode(exclude = {"owner"}) // to avoid StackOverflow
@ToString(exclude = {"owner"}) // // to avoid StackOverflow
public class Friend{
@NonNull
@EmbeddedId
private Key key = new Key();
@NonNull
@ManyToOne
@MapsId("ownerId")
private AppUser owner;
@NonNull
@ManyToOne
@MapsId("personId")
private AppUser person;
@NonNull
private boolean accepted;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public static class Key implements Serializable {
private Long ownerId;
private Long personId;
}
}
如果用户向其他用户发送好友请求记录如下:
[此时,一切正常,但我想隐藏向我发送朋友请求或向他发送朋友请求的用户的个人资料页面上的Add Friend
按钮的情况,我尝试使用[C0 ]在FriendRepository.java中的格式为:
JPQL
并且在服役中我有:
@Repository
public interface FriendRepository extends PagingAndSortingRepository<Friend, Long> {
@Query(value = "SELECT count(f) FROM friends f WHERE (f.owner.id = :id OR f.person.id = :id)")
Integer findPendingOrFriend(@Param("id") Long id);
}
在控制器中,我有:
public boolean findPendingOrFriend(Long id) {
// Find friends ids who/Whom friend requests are sent
return friendRepository.findPendingOrFriend(id) > 0;
}
但是它不能按预期方式工作,因为它为尚未向应用程序的任何其他用户发送任何好友请求的新用户隐藏了@GetMapping(value = "/{userId}/profile")
public String showProfile(
Model model,
@PathVariable(value = "userId") Long userId
) throws Exception {
// Send model
String pageTitle = "User " + Util.getLoggedInUser(userService).getUserFirstName() ;
model.addAttribute("pageTitle", pageTitle);
model.addAttribute("loggedInUser", Util.getLoggedInUser(userService));
model.addAttribute("profileUser", userService.findById(userId).orElseThrow(EntityNotFoundException::new));
// Check if pending or friend
boolean isPendingOrFriend = friendService.findPendingOrFriend(userId);
model.addAttribute("isPendingOrFriend", isPendingOrFriend);
return "user_profile";
}
按钮,并且在发送好友请求时,它始终显示Add Friend
按钮用户个人资料页面。问题是我该如何重写
Add Friend
以便它可以按照上面的要求工作。
您应该同时检查访客ID和所有者页面的ID
@Query(value = "SELECT count(f) FROM friends f WHERE (f.owner.id = :id OR f.person.id = :id)")
Integer findPendingOrFriend(@Param("id") Long id);
@Query(value = "SELECT count(f) FROM friends f WHERE ((f.owner.id = :id1 AND f.person.id = :id2) OR (f.owner.id = :id2 AND f.person.id = :id1))")
Integer findPendingOrFriend(@Param("id1") Long id1, @Param("id2") Long id2);
它看起来与@ sc0der的回答有点相似,但是它使用@Query(value = "SELECT count(f) FROM friends f WHERE (f.owner.id = :oId AND f.person.id = :pId) OR (f.person.id = :oId AND f.owner.id = :pId)") Integer findPendingOrFriend(@Param("oId") Long oId, @Param("pId") Long pId);
而不是AND
来检查条件,该条件很清楚。并在OR
中:
Service
和在控制器中:
public boolean findPendingOrFriend(Long oId, Long pId) { // Find friends ids return friendRepository.findPendingOrFriend(oId, pId) > 0; }
希望这可以按需工作。