@Entity
public class Person implements Serializable{
@Id
@GeneratedValue
private int id;
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "person")
private List<Car> cars;
//getters and setters
}
@Entity
public class Car implements Serializable{
@Id
@GeneratedValue
private int id;
private String name;
@ManyToOne
@JoinColumn(name = "person_id")
private Person person;
// getters and setters
}
而且..我这样使用它..
Person per = new Person();
per.setName("some");
Car car1 = new Car();
car1.setName("Ford");
Car car2 = new Car();
car2.setName("WagonR");
//s.save(car1);
//s.save(car2);
per.setCars(new ArrayList<Car>());
per.getCars().add(car1);
per.getCars().add(car2);
s.save(per);
现在..表格Car
有一个列person_id
,但它显示null
了两辆车..我在这里做错了什么?表格Person
已正确填写。如果我mappedBy
从Person
表中删除“”.. 而是包括@JoinColumn
这里...那么它工作正常。