Hibernate 遇到的问题(1)

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值