Hibernate 遇到的问题(1)

来源:互联网 发布:mac做铃声 编辑:程序博客网 时间:2024/06/08 00:05
  1. 错误Batch update returned unexpected row count from update [0]; actual row count: 0;
    ,Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1这个异常是由于主键设置为自增长,而在我们插入记录的时候设置了ID的值导致的。
  2. 刚刚使用SSH框架时,遇到了一个bug:
    原:applicationContext.xml
 <bean name="orderAction" class="Action.OrderAction" scope="prototype">        <property name="orderService" ref="OrderServiceImpl"/>    </bean>

原 OrderAction:

public class OrderAction extends SupAction{    private OrderServiceImpl orderService;    private UserServiceImpl userService;    private List<Order> orderList;    private List<User> userList;    public OrderServiceImpl getOrderService() {        return orderService;    }    public void setOrderService(OrderServiceImpl orderService) {        this.orderService = orderService;    }    public List<Order> getOrderList() {        return orderList;    }    public void setOrderList(List<Order> orderList) {        this.orderList = orderList;    }    public UserServiceImpl getUserService() {        return userService;    }    public void setUserService(UserServiceImpl userService) {        this.userService = userService;    }    public String ordermanage(){        System.out.println("OrderExec:Log");        this.orderList = this.orderService.fetchAll();        this.userList = this.userService.fetchAll();        System.out.println("User List Size:");        System.out.println(this.userList.size());        return SUCCESS;    }}

总的来讲,电脑都是按照顺序一步一步执行的,在写代码的时候,要明白流程,这个是很重要的,刚才的例子,我直接在 OrderAction 中更改,添加了userService,想着和OrderService一样,可以完成目标,但是忘记了我是使用Spring注入的Service,所以这里犯了第一个错误.然后第二个错误,没有给userList的get,set方法,因为在使用IDEA的代码生成功能时,求快,只生成了userService的set,get方法.
更改后:

   <bean name="orderAction" class="Action.OrderAction" scope="prototype">        <property name="orderService" ref="OrderServiceImpl"/>        <property name="userService" ref="UserServiceImpl"/>    </bean>public class OrderAction extends SupAction{    private OrderServiceImpl orderService;    private UserServiceImpl userService;    private List<Order> orderList;    private List<User> userList;    public OrderServiceImpl getOrderService() {        return orderService;    }    public void setOrderService(OrderServiceImpl orderService) {        this.orderService = orderService;    }    public List<Order> getOrderList() {        return orderList;    }    public void setOrderList(List<Order> orderList) {        this.orderList = orderList;    }    public UserServiceImpl getUserService() {        return userService;    }    public void setUserService(UserServiceImpl userService) {        this.userService = userService;    }    public List<User> getUserList() {        return userList;    }    public void setUserList(List<User> userList) {        this.userList = userList;    }    public String ordermanage(){        System.out.println("OrderExec:Log");        this.orderList = this.orderService.fetchAll();        this.userList = this.userService.fetchAll();        System.out.println("User List Size:");        System.out.println(this.userList.size());        return SUCCESS;    }}

写代码时要讲究流程,明确流程先后顺序.