![](/img/trans.png)
[英]How to map a collection of entities, inside an entity, using Hibernate?
[英]How to persist entity with collection inside by using Hibernate?
我正在尝试使用Hibernate ORM将实体保留在数据库中。 我的entity class
@Entity
@Table(name = "company")
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String name;
@OneToMany(mappedBy = "company")
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Employee> employees;
资料库
@Repository
@Transactional(propagation = Propagation.REQUIRED)
public class HibernateCompanyRepository implements CompanyRepository {
@Autowired
HibernateTemplate template;
@Override
public Company getCompany(int id) {
return template.get(Company.class, id);
}
@Override
public Collection<Company> getCompanies() {
return template.loadAll(Company.class);
}
@Override
public void addCompany(Company employee) {
template.save(employee);
}
@Override
public void deleteCompany(int id) {
template.delete(template.get(Company.class, id));
}
@Override
public void updateCompany(Company company) {
template.update(company);
}
}
这是我的控制器
@Controller
@RequestMapping("/company")
public class CompanyController {
@Autowired
CompanyRepository companyRepository;
@RequestMapping(value = "/{id}",
method = RequestMethod.GET,
produces = "application/json")
@ResponseBody public Company getCompany(@PathVariable("id") Integer id) {
return companyRepository.getCompany(id);
}
@RequestMapping(method = RequestMethod.PUT,
consumes = "application/json",
headers = "content-type=application/json")
public String updateCompany(@RequestBody Company company) {
companyRepository.updateCompany(company);
return "redirect:/company/" + company.getId();
}
// another methods ommited
}
旧的JSON
{
"name" : "IBM",
"employees" : [
{
"name" : "John",
"id" : 2
},
{
"name" : "Bill",
"id" : 4
}
],
"id" : 2
}
更新了JSON(清单已删除)
{
"name" : "IBM",
"employees" : [
{
"name" : "John",
"id" : 2
}
],
"id" : 2
}
我在updateCompany()
方法中收到一个Employee实体。 Entity
已成功更新。 它更改了Employee的集合(例如,我删除了一名雇员)。 但是Hibernate无法持久存储此更新的实体。 当我尝试检索它时,我看不到任何变化。 有什么建议如何解决吗?
UPD:
我正在尝试先更新已删除的员工
@Override
public void updateCompany(Company company) {
Company oldCompany = getCompany(company.getId());
for (Employee employee : oldCompany.getEmployees()) {
if (!company.getEmployees().contains(employee)) {
employee.setCompany(null);
template.update(employee);
}
}
template.update(company);
}
并获得例外
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [eu.flavio.restful.entity.Company#2]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [eu.flavio..restful.entity.Company#2]
Hibernate重要的是双向关联的所有者。 所有者端是不具有mappedBy
属性的端。 这就是寻找实体之间是否存在不存在关联的地方。
因此,此处的所有者是Employee.company
。 Company.employees
是相反的一面。 如果您希望Bill不再是IBM的一部分,则必须确保Bill的公司设置为null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.