我有两个拥有oneToMany关系的实体我试图坚持使用cascade.PERSIST这样拥有客户的汽车列表:
@Entity
public class Customer implements Serializable {
@Id
@Column(name = "id")
private String id;
@Column(name="name")
private String name;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "customer")
private List<Car> cars;
@Entity
public class Car implements Serializable {
@Id
@Column(name = "id")
private String id;
@Column(name="tupe")
private String type;
@ManyToOne(cascade = CascadeType.PERSIST)
private Customer customer;
在我的实体管理器中,我创建了一个允许创建和持久化实体Customer
的方法public Customer createCustomer(String name, List<Car> cars) {
Customer cust = new Customer();
cust.setName(name);
cust.setCars(cars);
em.persist(cust);
在我的JPADAOTest
中ArrayList<Cars> cars=carEjb.getCars();// getCars returns a list of Cars
Customer newcustomer=customerEJB.createCustomer("defaultName", cars);
运行我的应用程序后,客户已成功添加,但Car表中没有任何车辆。 我怎么能让它正常工作?