如何将另一个类对象添加到列表中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将另一个类对象添加到列表中相关的知识,希望对你有一定的参考价值。
我有一个播放器类和一个朋友类,播放器类中有一个朋友对象。但是,朋友类别也与玩家类别具有相同的字段。如何将好友列表添加到播放器。就像在Facebook上,用户的朋友列表中有朋友。如何使PlayerController类中的已注释方法起作用?
//@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping(value = "/players", produces = MediaType.APPLICATION_JSON_VALUE )
public class PlayerController
@Autowired
private PlayerReopsitory playerReopsitory;
@GetMapping(produces = "application/json")
public List<Player> getAllPlayers()
return playerReopsitory.findAll();
@PostMapping
// @ResponseStatus(HttpStatus.OK)
public void create(@RequestBody Player player)
playerReopsitory.save(player);
@GetMapping("/id")
public Player getOne(@PathVariable("id") Long id)
return playerReopsitory.getOne(id);
@DeleteMapping("/id")
void deletePlayer(@PathVariable Long id)
playerReopsitory.deleteById(id);
@PutMapping("/id")
Player updatePlayer(@RequestBody Player newPlayer, @PathVariable Long id)
return playerReopsitory.findById(id).map(player ->
player.setName(newPlayer.getName());
player.setEmail(newPlayer.getEmail());
player.setPhone(newPlayer.getPhone());
return playerReopsitory.save(player);
).orElseGet(() ->
newPlayer.setId(id);
return playerReopsitory.save(newPlayer);
);
// how to make this function work?
// @GetMapping("/id/friends")
// public List<Friend> getAllFriendFromList(@PathVariable Long id)
// return playerReopsitory.findById(id).get();
//
//
@Entity
@Table(name= "user", schema = "rabbit")
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
public class Friend
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="userName")
private String userName;
@Column(name="playername")
private String name;
@Column(name="email")
private String email;
@Column(name="phone")
private String phone;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
public String getUserName()
return userName;
public void setUserName(String userName)
this.userName = userName;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public String getPhone()
return phone;
public void setPhone(String phone)
this.phone = phone;
@Override
public String toString()
return "Friend [id=" + id + ", userName=" + userName + ", name=" + name + ", email=" + email + ", phone="
+ phone + "]";
@Entity
@Table(name= "user", schema = "rabbit")
@JsonIgnoreProperties("hibernateLazyInitializer", "handler")
public class Player
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="userName")
private String userName;
@Column(name="playername")
private String name;
@Column(name="email")
private String email;
@Column(name="phone")
private String phone;
private Friend friend;
public Friend getFriend()
return friend;
public void setFriend(Friend friend)
this.friend = friend;
public String getUserName()
return userName;
public void setUserName(String userName)
this.userName = userName;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public String getPhone()
return phone;
public void setPhone(String phone)
this.phone = phone;
public Long getId()
return id;
public void setId(Long id)
this.id = id;
@Override
public String toString()
return "Player [id=" + id + ", userName=" + userName + ", name=" + name + ", email=" + email + ", phone="
+ phone + ", friend=" + friend + "]";
public interface FriendReopsitory extends JpaRepository<Player, Long>
public interface PlayerReopsitory extends JpaRepository<Player, Long>
@Component
public class FriendService
private static Map<Friend, Long> getFriends = new HashMap<>();
答案
[因为朋友也是玩家,所以我建议您将模型重构为只有一个类别的玩家,并且与自己之间存在多对多的关系。
@Entity
public class Player
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/*
* Add other fields
*/
@ManyToMany()
@JoinTable(name = "player_friends", joinColumns =
@JoinColumn(name = "PLAYER_ID", referencedColumnName = "ID")
, inverseJoinColumns =
@JoinColumn(name = "FRIEND_ID", referencedColumnName = "ID")
)
private List<Player> friends;
/*
* Add other getters and setters
*/
public List<Player> getFriends()
return this.friends;
/*
* Implement addFriend and removeFriend methods
*/
然后您将需要处理集合加载(LAZY或EAGER),添加和删除好友方法。但这不在此问题的范围内,您可以在线找到有关此主题的大量资源。
以上是关于如何将另一个类对象添加到列表中的主要内容,如果未能解决你的问题,请参考以下文章
在java中,在一个类中取Application的对象如何取?