我有 2 节课
@Entity
@Table(name = "user")
public class User
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
@Cascade({CascadeType.ALL})
private Collection<Book> userBooks;
}
@Entity
@Table(name = "books")
public class Book
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@ManyToOne
private User user;
}
这里还有一些代码:
User user = new User(); user.setUserName("Gleeb"); user.setUserPassword("gleeb");
Book book = new Book();
book.setTitle("some book ");
book.setbookType(1);
user.addUserbook(book);
userService.save(user);
我正在尝试创建一个新用户并向其中添加一本新书。当我保存用户时
session.saveOrUpdate(用户);
保存了用户,保存了书,但在书表中,未设置 user_id。并留空。
这是为什么。
谢谢。