I have similar problem as is described in Spring Framework – New object is created in DB instead of updating but I have two entities: Author and Department in @ManyToOne relation. One department has many authors and one author belongs to one department. When I update an author, author details are updated, but department is inserted which leads to duplicate entries. From aforementioned link I know that I’m probably missing department ID, but I don’t know how to tie department with its ID.
Author entity (without getters and setters)
@Entity
@Table(name = "author")
public class Author implements Serializable {
@Id
@Column(name = "idauthor")
@GeneratedValue
private Integer idAuthor;
@Column(name = "name")
private String name;
@ManyToOne(cascade = CascadeType.ALL)
private Department department;
}
Department entity (without getters and setters)
@Entity
@Table(name="department")
public class Department implements Serializable {
@Id
@Column(name="iddepartment")
@GeneratedValue
private Integer iddepartment;
@Column(name="name", unique=true)
private String name;
}
Methods for update operatio of Author in AuthorController
@Controller
@RequestMapping("/author")
public class AuthorController {
@Autowired
private AuthorService authorService;
@Autowired
private DepartmentService departmentService;
@RequestMapping(value="/save/{authorId}", method = RequestMethod.GET)
public String renderUpdateForm(@PathVariable("authorId") Integer authorId, ModelMap model) {
model.addAttribute("author", authorService.getAuthorByID(authorId));
model.addAttribute("departmentList", departmentService.listDepartment());
return "author.save";
}
@RequestMapping(value="/save/{authorId}", method = RequestMethod.POST)
public String updateAuthor(@ModelAttribute("idAuthor") Author author, BindingResult result, SessionStatus status) {
authorService.updateAuthor(author);
status.setComplete();
return "redirect:/author";
}
}
authorService.updateAuthor(author); calls service which calls DAO class with method:
@Repository
@Transactional
public class AuthorDAOImpl implements AuthorDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public void updateAuthor(Author author) {
sessionFactory.getCurrentSession().update(author);
}
}
and finally JSP with form
<form:form method="post" action="${pageContext.request.contextPath}/author/save" modelAttribute="author">
<form:hidden path="idAuthor" />
<table>
<tr>
<td>
<form:label path="name">
<spring:message code="label.author.name"/>
</form:label>
</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>
<form:label path="department.name">
<spring:message code="label.author.department"/>
</form:label>
</td>
<td>
<form:select path="department.name">
<form:options items="${departmentList}" itemValue="name" itemLabel="name" />
</form:select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="<spring:message code="label.update" />
</td>
</tr>
</table>
</form:form>
And the error I’m getting is
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry ‘custom department
name’ for key ‘name’
How should I pass ID to department or fix my problem? Thanks for your advises.
I had to add Formatters SPI (see documentation for more information) for each POJO. Then hibernate updated objects instead of saving duplicity.