我有这个数据库结构。有一些用户每年阅读书籍,并汇编他们阅读过的书籍清单:
users years books----booklists
| | |
------------------------------------
|
userlists
我有一个用户的表:
users:
+-----------+---------+
| id | name |
+-----------+---------+
@Entity
public class User {
@Id @GeneratedValue
private int id;
//?
private List<UserList> booklists = new ArrayList<>();
}
每个用户都有他们在特定年份已阅读 的图书的列表:
books:
+-----------+---------+
| id | title |
+-----------+---------+
@Entity
public class Book {
@Id @GeneratedValue
private int id;
}
每个用户的列表位于 booklists 表中:
booklists:
+-----+---------+---------+
| id | list_id | book_id |
+-----+---------+---------+
@Entity
public class BookList {
@Id @GeneratedValue
private int id;
@Column(name="list_id")
private int listId;
@Column(name="book_id")
private int bookId;
}
最后,书单和用户被合并到用户列表:
years:
+-----+------+
| id | year |
+-----+------+
userlists:
+-----+---------+---------+---------+
| id | user_id | list_id | year_id |
+-----+---------+---------+---------+
@Entity
public class UserList {
@Id @GeneratedValue
private int id;
@Column(name="user_id")
private int userId;
@Column(name="list_id")
private int listId;
@Column(name="year_id")
private int yearId;
}
我不确定如何在private List<UserList> booklists = new ArrayList<>();
中使用User
。我知道一对多可以这样映射:
@OneToMany
@JoinTable(joinColumns=@JoinColumn(name="user_id"),
inverseJoinColumns=@JoinColumn(name="list_id")
List<UserList> booklists = new ArrayList<>();
但是我遇到的情况比我看过的任何教程都多。