以下是我的控制器方法:
\Stripe\Plan::create(array(
"amount" => 100,
"interval" => "month",
"product" => array(
"name" => "Gold Metered"
),
"currency" => "usd",
"id" => "gold-metered",
"usage_type" => "metered",
"aggregate_usage" => "last_during_period"
));
这是我的GET方法,当我单击“更新”按钮时,它显示了一个具有来自MYSQL DB的实体信息的表单
@RequestMapping(value="/updateApi", method=RequestMethod.POST)
public String updateApiPost(@ModelAttribute("api") Api api, HttpServletRequest request) {
adminAppLogger.info("Attempting to update api with id {}:"+api.getId()+" title: {}"+api.getTitle());
apiService.save(api);
MultipartFile apiImage = api.getApiImage();
if(!apiImage.isEmpty()) {
try {
byte[] bytes = apiImage.getBytes();
String name = api.getId() + ".png";
Files.delete(Paths.get("src/main/resources/static/image/api/"+name));
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File("src/main/resources/static/image/api/" + name)));
stream.write(bytes);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
adminAppLogger.info("Successfully updated api with title {}:"+api.getTitle()+" {"+api+"}");
return "redirect:/api/apiInfo?id="+api.getId();
}
我希望它更新db,但是它将在db中插入一个全新的条目:日志是: Hibernate:插入api_info(活动,作者,类别,代码,日期,描述,data_json,number_of_lines,进度,项目,response_json,title,url)值(?,?,?,?,?,?,?,?, ?,?,?,?,?)
以下是我的服务和存储库:
@RequestMapping("/updateApi")
public String updateApi(@RequestParam("id") Long id, Model model) {
Api api = apiService.findOne(id);
model.addAttribute("api", api);
return "updateApi";
}
存储库:
@Service
public class ApiServiceImpl implements ApiService{
@Autowired
private ApiRepository apiRepository;
public Api save(Api api) {
return apiRepository.save(api);
}
public List<Api> findAll() {
return (List<Api>) apiRepository.findAll();
}
public Api findOne(Long id) {
return apiRepository.findOne(id);
}
public void removeOne(Long id) {
apiRepository.delete(id);
}
}
答案 0 :(得分:2)
如果您在期望发生更新的位置插入INSERT,则表示:
save
的每个实体都没有设置ID,因此persist
被称为幕后成员检查实体是否设置了ID字段。
由于我没有在插入字段列表中看到id字段,因此我认为它缺少ID,因为否则它将包含在insert中。