Spring roo Hibernate更新入口多对多映射
我在 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();
,我会看到人员表中反映的变化。我做错了什么?谢谢
I have created a many to many mapping in spring roo between two entities Person and car and as expected I see a new table Person_Car. Below is the code in Person.java
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "persons")
private Set<Car> cars= new HashSet<Car>();
Now I want to add entries in the table. I set the cars to the person object and do 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);
}
However, no entries are being made in the Person_Car table. But, when I do something like
Person person = Person.findPerson(personId);
person.setName("Jonathan");
person.merge();
I see the change reflected in the person table. What am I doing wrong? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
@Transactional(rollback=false)
添加到您的控制器方法中。rollback=false
是默认值,因此您可以跳过它,只剩下@Transactional
。Try to add
@Transactional(rollback=false)
to your controller method.rollback=false
is default so you can skip it, and only@Transactional
is left.