通过Spring中的表单更新db数据插入重复项

时间:2011-06-26 21:02:23

标签: hibernate spring spring-mvc

我遇到的问题与Spring Framework - New object is created in DB instead of updating中描述的类似,但我有两个实体:AuthorDepartment @ManyToOne关系。一个部门有很多作者,一个作者属于一个部门。当我更新作者时,会更新作者详细信息,但会插入部门,从而导致重复的条目。从上述链接我知道我可能缺少部门ID,但我不知道如何将部门与其ID联系起来。

作者实体(没有getter和setter)

@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;
}

部门实体(没有吸气剂和制定者)

@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;
}

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);调用使用方法调用DAO类的服务:

@Repository
@Transactional
public class AuthorDAOImpl implements AuthorDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void updateAuthor(Author author) {
        sessionFactory.getCurrentSession().update(author);
    }
}

最后是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>

我得到的错误是

  

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   重复输入'自定义部门   名称'为密钥'名称'

我应该如何将ID传递给部门或解决我的问题?谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

我必须为每个POJO添加Formatters SPI(有关详细信息,请参阅documentation)。然后hibernate更新对象而不是保存双重性。