我目前正在学习休眠方式,并创建了一个模拟库的简单数据库:
我有3个班级:预订,客户,订单:
@Entity
public class Book
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private int ISBN;
private String name;
private String autorName;
private double price;
//Getters setters etc...
}
@Entity
public class Customer
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String lastName;
private String city;
//Getters setters etc...
}
@Entity
@Table(name = "TB_order")
public class Order
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Date date;
private String status;
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn
private List<Book> books;
@ManyToOne(cascade = CascadeType.ALL)
private Customer customer;
//Getters setters etc...
}
当我尝试通过saveOrUpdate()更改某些订单时(例如,更改客户订购的书),我创建了Order的新实例,并将其传递给saveOrUpdate()方法:
Order o = new Order(Date.valueOf("2019-06-01"),"old",bookslist.subList(2,3), customerslist.get(0));
o.setId(1);
session.saveOrUpdate(o);
它不仅按表顺序更改值,而且还在表簿和表客户中创建新实体。我猜是这样的,因为我把在主方法中创建的书籍清单和客户实例安装了。
那么应该将Order对象的哪个实例传递到saveOrUpdate()方法中,以仅将表Order中的值更改为数据库中已经存在的值?