我有两个实体:Client和RiskPerson。客户有价值可计算他添加的RiskPersons数量。我检查当前客户端是否添加了一个新的Person然后增加该值,否则只保存。在此过程中,当前Person已存在时,我收到错误:
org.springframework.web.util.NestedServletException:Request 处理失败;嵌套异常是 org.hibernate.NonUniqueObjectException:一个不同的对象 相同的标识符值已与会话关联: [com.middle.fpdb.entities.RiskPerson#14]
我会很乐意帮忙
RiskPersonServiceImpl
...
@Override
@Transactional
public void savePerson(RiskPerson riskPerson) {
Date date = new Date();
date = new Timestamp(System.currentTimeMillis());
Client client = clientService.getClient(2); // EMULATION OF CLIENT
riskPerson.setClient(client); // EMULATION OF CLIENT
riskPerson.setTimestamp(date);
if (!riskPersonDAO.personExists(riskPerson.getId()))
clientService.incrementClientPeople(2);
riskPersonDAO.saveRiskPerson(riskPerson);
}
...
RiskPersonDAO
...
@Override
@Transactional
public boolean personExists(int id) {
Session session = sessionFactory.getCurrentSession();
RiskPerson person = session.get(RiskPerson.class, id);
if (person == null)
return false;
else
return true;
}
...
ClientService和客户端DAO
//DAO METHOD
@Override
public void incrementClientPeople(int id) {
Session session = sessionFactory.getCurrentSession();
Client client = session.get(Client.class, id);
client.setCountAddedPeople(client.getCountAddedPeople()+1);
}
//Service method
@Override
@Transactional
public void incrementClientPeople(int id) {
clientDAO.incrementClientPeople(id);
}