Java中如何表示多对多的关系

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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("西瓜"));

 

posted @   Code_Rush  阅读(986)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示