我有一个问题,我想用下面的例子来解释:
JPA 类人:
@ManyToMany(mappedBy = "persons")
private List<Car> cars= new ArrayList<>();
public void setCars(List<Car> cars) {
this.cars= cars;
}
public List<Car> getCars() {
return cars;
}
以及问题出现的地方:
小服务程序类:
Person person = genericDao.findOne(1);
request.setAttribute("person", person.getName());
List<Car> cars= new ArrayList<Car>();
cars= person.getCars();
request.setAttribute("cars", cars);
问题是,当试图让汽车。它们不会自动从数据库中检索并保存在 Person 类的数组中。如何设置具有多对多关系的 JPA 类,从中我可以轻松地检索具有关系的对象。
汽车.java
进口被遗漏了!
@Entity
@Table(name = "DSD_Cars")
public class Car extends ModifiableEntity {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToMany
@JoinTable(
name="DSD_Persons_Cars",
joinColumns = {@JoinColumn(name="IDCar", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "IDPerson", referencedColumnName = "ID")}
)
private List<Person> persons= new ArrayList<>();
public void setPersons(List<Person> persons) {
this.persons= persons;
}
public List<Persons> getPersons() {
return persons;
}}