Spring roo Hibernate更新入口多对多映射

发布于 2025-01-06 19:19:18 字数 1303 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2025-01-13 19:19:18

尝试将 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文