当前位置:编程学习 > JAVA >>

求帮:JPA外键关联,进行修改外键时出错identifier of an instance of …… was altered from

求帮:JPA外键关联,进行修改外键时出错identifier of an instance of …… was altered from

identifier of an instance of A表 was altered from 2 to 3 

A表【id,name】 
B表【id,name,desc,a_id(外键)】 


页面是select  name=a.id 

B{ 
ManyToOne 
A a; 


save时 会报上面的错,我是不想修改A表 信息的,只想改B表的外键的值 

求帮助哈…………,感谢!  JPA identifier of --------------------编程问答-------------------- 修改的代码呢,看看,主键是不能修改的,你修改了主键 --------------------编程问答-------------------- 你用update吧。还有你最好把代码粘出来看看。 --------------------编程问答-------------------- controller:
@RequestMapping(value="/update", method=RequestMethod.POST)
public String update(@ModelAttribute("preloadSite")SiteInfo siteInfo, RedirectAttributes redirectAttributes) {
siteInfoService.update(siteInfo);
redirectAttributes.addFlashAttribute("message", "修改成功!");
return "redirect:/" + LIST;
}

Service:
@Transactional
@Override
public void update(T entity) {
jpaRepository.save(entity);
}


对啊,我是不想修改  A表的主键,只想修改B表的外键的值,第一次用jpa ,请多指教
A表:
@OneToMany(cascade=CascadeType.PERSIST, mappedBy="companyInfo")
private List<SiteInfo> siteInfos = Lists.newArrayList();
B表:
    @ManyToOne
@JoinColumn(name="company_id")
private CompanyInfo companyInfo;


--------------------编程问答-------------------- public void update(T entity) {
jpaRepository.save(entity);
这里应该update吧,不应该save了
} --------------------编程问答-------------------- 页面代码:

<select id="companyInfo.id" name="companyInfo.id"> --------------------编程问答-------------------- 如果controller 用这个:
@RequestMapping(value="/update", method=RequestMethod.POST)
public String update(@ModelAttribute("preloadSite")SiteInfo siteInfo, Long pid, RedirectAttributes redirectAttributes) {
    CompanyInfo ci = new CompanyInfo();
    ci.setId(pid);
    siteInfo.setCompanyInfo(ci);
siteInfoService.update(siteInfo);
redirectAttributes.addFlashAttribute("message", "修改成功!");
return "redirect:/" + LIST;
}
页面代码:
<select id="companyInfo.id" name="pid">

就没问题了,不过这样觉得  不是根本解决问题的原因。



jpaRepository  是没有update 方法的 --------------------编程问答-------------------- 加上CascadeType.REMOVE,取消级联更新。 --------------------编程问答--------------------
引用 7 楼 fangmingshijie 的回复:
加上CascadeType.REMOVE,取消级联更新。


@ManyToOne(cascade=CascadeType.REMOVE)
@JoinColumn(name="company_id")
private CompanyInfo companyInfo;

这个不行 --------------------编程问答-------------------- 终于可以了,感谢两位了!

解决方法:

@ModelAttribute("preloadSite")
public SiteInfo getOne(@RequestParam(value = "id", required = false) Long id) {
if (id != null) {
SiteInfo siteInfo = siteInfoService.get(id);
siteInfo.setCompanyInfo(null);
            return siteInfo;
}
return null;
} --------------------编程问答--------------------
引用 9 楼 dearbingo 的回复:
终于可以了,感谢两位了!

解决方法:

@ModelAttribute("preloadSite")
public SiteInfo getOne(@RequestParam(value = "id", required = false) Long id) {
if (id != null) {
SiteInfo siteInfo = siteInfoService.get(id);
siteInfo.setCompanyInfo(null);
            return siteInfo;
}
return null;
}

你这是手动取消关联了,太蛋疼了吧 --------------------编程问答-------------------- 还有啥别的解决方案,求解啊!

确实蛋疼啊……
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,