hibernate get id = 0批量更新从update [0]返回意外行数;实际行数:0;预期:1

时间:2016-05-14 10:33:33

标签: java spring hibernate jsp ssl

我有使用hibernate + spring编辑的错误。我已经在搜索了这个。但还是找不到解决方案。我编写C R U D代码,所有工作都很好,如添加/删除,但在编辑/更新数据库时出错。

请帮我找一个解决方案吗?
感谢。

这是我的代码:

模态/实体EmployeStatus.java

@Entity
@Table
public class EmployeeStatus {

    @Id
    @Column
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int id;

    @Column
    private String name;

    public EmployeeStatus() {
    }

    public EmployeeStatus(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "EmployeeStatus [id=" + id + ", name=" + name + "]";
    }

}

Dao impl EmployeeStatusDaoImpl.java

@Repository
public class EmployeeStatusDaoImpl implements EmployeeStatusDao {

private static final Logger logger = LoggerFactory.getLogger(EmployeeStatusDaoImpl.class);

@Autowired
private SessionFactory sessionFactory;

public EmployeeStatus findById(int id) {

    return (EmployeeStatus)sessionFactory.getCurrentSession().get(EmployeeStatus.class, id);
}

@SuppressWarnings("unchecked")
public List<EmployeeStatus> findAll() {
    Session session = this.sessionFactory.getCurrentSession();
    List<EmployeeStatus> employeeList = session.createQuery("from EmployeeStatus").list();
    return employeeList;
}


public void save(EmployeeStatus es) {
    //sessionFactory.getCurrentSession().save(es);
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(es);
}


public void update(EmployeeStatus es) {
    //Session session = this.sessionFactory.getCurrentSession();
    //session.update(es);
    sessionFactory.getCurrentSession().update(es);
    sessionFactory.getCurrentSession().flush();
    //sessions.getCurrentSession().update(es);
    logger.info("Employee Status updated successfully, Employee Status Details="+es);
}


public void delete(int id) {
    sessionFactory.getCurrentSession().delete(findById(id));

}


}

service impl EmployeeStatusServiceImpl.java

@Service
public class EmployeeStatusServiceImpl implements EmployeeStatusService{

@Autowired
private EmployeeStatusDao employeeStatusDao;

@Transactional
public EmployeeStatus findById(int id) {
    return employeeStatusDao.findById(id);
}

@Transactional
public List<EmployeeStatus> findAll() {
    return employeeStatusDao.findAll();
}

@Transactional
public void save(EmployeeStatus es) {
    employeeStatusDao.save(es);

}

@Transactional
public void update(EmployeeStatus es) {
    employeeStatusDao.update(es);

}

@Transactional
public void delete(int id) {
    employeeStatusDao.delete(id);

}


}

控制器EmployeeStatusController.java

@Controller
public class EmployeeStatusController {

@Autowired
private EmployeeStatusService employeeStatusService;

@RequestMapping(value="employeeStatus", method = RequestMethod.GET)
public String list(Model model) {
    model.addAttribute("empStat", new EmployeeStatus());
    model.addAttribute("list", this.employeeStatusService.findAll());
    return "empStat";
}



    //For add 
    @RequestMapping(value= "/add", method = RequestMethod.POST)
    public String addEmployeeStatus(@ModelAttribute("empStat") EmployeeStatus es){
        //new employee status, call save
        this.employeeStatusService.save(es);
        return "redirect:/employeeStatus";

    }

    //for edit
    @RequestMapping(value= "/update", method = RequestMethod.POST)
    public String updateEmployeeStatus(@ModelAttribute("empStat") EmployeeStatus es){

        //existing Employee status, call update function from service

        employeeStatusService.update(es);
        return "redirect:/employeeStatus";

    }

    @RequestMapping("/remove/{id}")
    public String removeEmployeeStatus(@PathVariable("id") int id){

        this.employeeStatusService.delete(id);
        return "redirect:/employeeStatus";
    }

    //for call edit in list
        @RequestMapping("/edit/{id}")
        public String editEmployeeStatus(@PathVariable("id") int id, Model model){

            model.addAttribute("list",   this.employeeStatusService.findAll());
            model.addAttribute("empStat", this.employeeStatusService.findById(id));
        return "empStat";
    }


}

jsp employeeStatus.jsp

   ` <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>


    <div id="page-wrapper">
    <div class="container-fluid">

        <!-- Page Heading -->
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Data Master</h1>
                <ol class="breadcrumb">
                    <li><i class="fa fa-dashboard"></i> <a href="/">Dashboard</a>
                    </li>
                    <li class="active"><i class="fa fa-table"></i> Employee Status List</li>
                </ol>
            </div>
        </div>
        <!-- /.row -->
        <div class="page-content">
            <div class="col-md-9">
                <div class="panel panel-green">
                    <div class="panel-heading">Employee Status</div>
                    <div class="panel-body">
                        <table class="table table-hover table-bordered">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Name</th>
                                    <th>Operation</th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach items="${list}" var="empStatlist">
                                    <tr>
                                        <td><a><c:out value="${empStatlist.id}" /></a></td>
                                        <td><a><c:out value="${empStatlist.name}" /></a></td>
                                        <td align="center">
                                            <a href="<c:url value='/edit/${empStatlist.id}' />">
                                                <span class="glyphicon glyphicon-edit"></span>
                                            </a> 
                                                    
                                            <a href="<c:url value='/remove/${empStatlist.id}' />">
    <span class="glyphicon glyphicon-trash"></span>
                                            </a>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                </div>

                <br>
                <div class="panel panel-success">
                            <div class="panel-heading">
                                <h3 class="panel-title">Add Employee Status</h3>
                            </div>
                            <div class="panel-body">
                                <c:url var="addAction" value="/add"></c:url>

                                <form:form action="${addAction}" commandName="empStat">
                                    <table>
                                        <tr>
                                            <td><form:label path="name">
                                                    <spring:message text="Name" />
                                                </form:label>
                                            </td>
                                            <td>
                                                <form:input path="name" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                <input type="submit" value="<spring:message text="Add"/>" />
                                            </td>
                                        </tr>
                                    </table>
                                </form:form>
                            </div>
                        </div>

                        <br>
                        <div class="panel panel-success">
                            <div class="panel-heading">
                                <h3 class="panel-title">Edit Employee Status</h3>
                            </div>
                            <div class="panel-body">
                                <c:url var="editAction" value="/update"></c:url>

                                <form:form action="${editAction}" commandName="empStat">
                                    <table>

                                            <tr>
                                                <td>
                                                    <form:label path="id" >
                                                        <spring:message text="ID" />
                                                    </form:label>
                                                </td>
                                                <td>
                                                    <!-- <form:input path="id" />  -->
                                                    <form:input path="id" readonly="true" size="8"   disabled="true" />
                                                </td>
                                            </tr>

                                        <tr>
                                            <td><form:label path="name">
                                                    <spring:message text="Name" />
                                                </form:label>
                                            </td>
                                            <td>
                                                <form:input path="name" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                    <input type="submit" value="<spring:message text="Update"/>" />
                                            </td>
                                        </tr>
                                    </table>
                                </form:form>
                            </div>
                        </div>
            </div>
        </div>                
    </div>
    </div>

当我点击simbol编辑按钮时,我得到数据,如id和name get id

但是当我点击编辑按钮时出现此错误

15:48:18,574 DEBUG AnnotationTransactionAttributeSource:108 - Adding     transactional method 'EmployeeStatusServiceImpl.update' with attribute:     PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Sat May 14 15:48:18 WIB 2016 WARN: Establishing SSL connection without     server's identity verification is not recommended. According to MySQL     5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be     established by default if explicit option isn't set. For compliance with     existing applications not using SSL the verifyServerCertificate property     is set to 'false'. You need either to explicitly disable SSL by setting     useSSL=false, or set useSSL=true and provide truststore for server    certificate verification.
15:48:18,587  INFO EmployeeStatusDaoImpl:50 - Employee Status updated successfully, Employee Status Details=EmployeeStatus [id=0, name=Semi Permanent]
Hibernate: 
    update
        EmployeeStatus 
    set
        name=? 
    where
        id=?
15:48:18,603 ERROR AbstractBatcher:73 - Exception executing batch: 
org.hibernate.StaleStateException: Batch update returned unexpected row</code>

id = 0为什么?

我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

请在EmployeeStatus.java中执行以下操作

  1. 更改private int id;到private Long Id

  2. 删除此id属性的setter方法。

答案 1 :(得分:0)

如果你的jsp生成的HTML在id输入中包含禁用,你的答案是

  

带有已禁用元素属性未提交或您可以说   他们的价值没有公布。

更多信息here