在数据库中表示多对多的关系可以采取连接表,那么在Java中能不能表示多对多的关系呢?应该如何表示呢?下面提供一种方案:

public class Category
{
	private int id;
	private String name;
	private Set<Item> items;
}

public class Item
{
	private int id;
	private String name;
	private Set<Category> categories;
}

Category category = new Category();
category.setId(1);
category.setName("水果");
category.setItems(new HashSet<Item>());
category.getItems().add(new Item("苹果"));
category.getItems().add(new Item("西瓜"));

 

相关文章:

  • 2022-01-01
  • 2021-10-09
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2021-05-14
  • 2021-11-05
  • 2022-12-23
猜你喜欢
  • 2021-12-11
  • 2022-12-23
  • 2021-05-26
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案