【发布时间】:2012-02-20 04:37:08
【问题描述】:
我在 spring roo 中创建了两个实体 Person 和 car 之间的多对多映射,正如预期的那样,我看到了一个新表 Person_Car。下面是 Person.java 中的代码
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "persons")
private Set<Car> cars= new HashSet<Car>();
现在我想在表格中添加条目。我将汽车设置为 person 对象并执行 person.merge()。
@RequestMapping(value ="/saveCars", method = RequestMethod.POST)
@ResponseBody
public String saveCars(@RequestParam("personId")Long personId, @RequestParam("cars") String incomingCars){
Map<String, Object> response = new HashMap<String, Object>();
try{
...
Person person = Person.findPerson(personId);
Set<Car> cars = person.getCars();
for(int i=0 ; i<temp.length ; i++){
carID = Long.parseLong(temp[i]);
cars.add(Car.findCar(carID));
}
person.setCars(cars);
person.merge();
}
LOG.debug("cars successfully added to questionnaire");
response.put("message", "success");
}
return new JSONSerializer().exclude("*.class").deepSerialize(response);
}
但是,没有在 Person_Car 表中创建任何条目。但是,当我做类似的事情时
Person person = Person.findPerson(personId);
person.setName("Jonathan");
person.merge();
我看到人员表中反映的变化。我究竟做错了什么?谢谢
【问题讨论】:
标签: java hibernate spring spring-mvc spring-roo