开发者社区> 问答> 正文

用hibernate在修改一条数据时没有覆盖旧的记录,反而添加了一条新纪录,怎么回事?

我用的是mysql的数据库,
jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML>





    <div class="form-group">
    <div class="col-sm-10">
    <center><h3>编辑部门信息</h3></center>
    </div>
    </div>



    <div class="form-group">
    <label for="dpname" class="col-sm-2 control-label">部门名称:</label>
    <div class="col-sm-6">
    <input name="department.dpname" type="text" class="form-control" id="dpname" value="${department.dpname}">
    </div>
    </div>

    <div class="form-group">
    <label for="dpnum" class="col-sm-2 control-label">部门人数:</label>
    <div class="col-sm-6">
    <input name="department.dpnum" type="number" class="form-control" id="dpnum" value="${department.dpnum}">
    </div>
    </div>

    <div class="form-group">
    <label for="dpmanager" class="col-sm-2 control-label">部门经理:</label>
    <div class="col-sm-6">
    <input name="department.dpmanager" type="text" value="${department.dpmanager}" class="form-control" id="dpmanager">
    </div>
    </div>


    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-6">
            <button type="submit" class="btn btn-default">
            提交</button>
        </div>
    </div>
</form>

</div>
<script src="js/jquery-1.11.1.min.js" ></script>
<script src="js/bootstrap.min.js" ></script>

action
package com.admin.actions;
import hbn.Department;
import hbn.DepartmentDAO;
import java.util.List;
import org.hibernate.Transaction;
public class DeptAction {
        private Department department;
        private List dlist;
        private String dmessage;

        public Department getDepartment() {
            return department;
        }
        public void setDepartment(Department department) {
            this.department = department;
        }

        public List getDlist() {
            return dlist;
        }
        public void setDlist(List dlist) {
            this.dlist = dlist;
        }
        public String getDmessage() {
            return dmessage;
        }
        public void setDmessage(String dmessage) {
            this.dmessage = dmessage;
        }

        public String deptlist() {

            DepartmentDAO dd = new DepartmentDAO();
            Transaction tx = dd.getSession().beginTransaction();
            dlist = dd.findAll();
            tx.commit();
            dd.getSession().flush();
            String result = "deptlist";
            return result;
        }



        public String deptupdate() {
            String result;
            try {
                DepartmentDAO dd = new DepartmentDAO();
                department = dd.findById(department.getId());
                dd.getSession().beginTransaction().commit();
                result = "deptupdate";
            } catch (Exception e) {
                // TODO: handle exception
                dmessage = "无法编辑该部门信息!";
                result = "dmessage";
            }
            return result;
        }

        public String deptup() {
            String result;
            try {
                DepartmentDAO dd = new DepartmentDAO();
                Transaction tx = dd.getSession().beginTransaction();
                System.out.println(department);
                dd.merge(department);
                tx.commit();
                result = "success";
            } catch (Exception e) {
                // TODO: handle exception
                dmessage = "部门信息修改失败!";
                result = "error";
            }

            return result;
        }
}

求大家帮忙看看!

展开
收起
云栖技术 2016-06-02 13:55:29 2924 0
1 条回答
写回答
取消 提交回答
  • 社区爱好者,专为云栖社区服务!

    在 deptup() 里面,用id从数据库中再取一次department对象。
    然后把action的department的每个field复制到刚刚获取的对象中去。
    最后在同一个transaction里保存刚才获取的department对象。

    2019-07-17 19:24:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
事务、全局索引、透明分布式 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载