把开发过程中碰到的BUG累积下来也是一笔财富。
网络收集:
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
不注意的话,还真的有点无所适从,Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1这个异常是由于主键设置为自增长,而在我们插入记录的时候设置了ID的值导致的。
Hibernate 注释@OneToOne 建立表关联实例
我的解决方案:
我是在做One-To-One级联 添加/更新 碰到的,开始还以为是我配置错误,后面经过仔细排查,发现是我在页面上写了子表ID的隐藏表单域,传到后面的时候value="" ,不是value=null,所以执行的时候hibernate判断为更新,所以报错了。我的解决方案是把ID单独传到后台,然后用代码判断是否为空,然后进行更新或者增加。
下面是我的代码:
TranOrders.java(父类):
- @Entity
- @Table(name = "TSPRO_TRAN_ORDERS")
- public class TranOrders implements java.io.Serializable {
- // Fields
- private TranItem tranItem;
- private TranCar tranCar;
- /** default constructor */
- public TranOrders() {
- }
- @OneToOne(cascade=CascadeType.ALL,mappedBy="tranOrders",fetch=FetchType.LAZY,optional = true)
- @JoinColumn(name="id",unique=true)
- public TranItem getTranItem() {
- return tranItem;
- }
- public void setTranItem(TranItem tranItem) {
- this.tranItem = tranItem;
- }
- @OneToOne(cascade=CascadeType.ALL,mappedBy="tranOrders",fetch=FetchType.LAZY,optional = true)
- @JoinColumn(name="id",unique=true)
- public TranCar getTranCar() {
- return tranCar;
- }
- public void setTranCar(TranCar tranCar) {
- this.tranCar = tranCar;
- }
- }
TranItem.java(子类)
- @Entity
- @Table(name = "TSPRO_TRAN_ITEM")
- public class TranItem implements java.io.Serializable {
- // Fields
- private TranOrders tranOrders;
- @OneToOne(fetch = FetchType.LAZY,optional=false)
- @JoinColumn(name = "TSP_ID",unique=true)
- public TranOrders getTranOrders() {
- return this.tranOrders;
- }
- public void setTranOrders(TranOrders tranOrders) {
- this.tranOrders = tranOrders;
- }
- }
TranCar.java(子类)
- @Entity
- @Table(name = "TSPRO_TRAN_CAR")
- public class TranCar implements java.io.Serializable {
- // Fields
- private TranOrders tranOrders;
- @OneToOne(fetch = FetchType.LAZY,optional=false)
- @JoinColumn(name = "TSP_ID",unique=true)
- public TranOrders getTranOrders() {
- return this.tranOrders;
- }
- public void setTranOrders(TranOrders tranOrders) {
- this.tranOrders = tranOrders;
- }
- }
Action(调用):
- public String saveTranOrders() {
- String tranItemId=super.getRequest().getParameter("tranItemId");
- String tranCarId=super.getRequest().getParameter("tranCarId");
- String loginUserId = (String) getSession().getAttribute(Constant.LOGIN_USER_ID);
- //时间转换
- String loadSort=super.getRequest().getParameter("loadSort");
- String reachTime=super.getRequest().getParameter("reachTime");
- String wzdTime=super.getRequest().getParameter("wzdTime");
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日HH时");
- try {
- if(tranOrders!=null){
- tranOrders.setLoadSort(new Timestamp(sdf.parse(loadSort).getTime()));
- tranOrders.setReachTime(new Timestamp(sdf.parse(reachTime).getTime()));
- tranOrders.setWzdTime(new Timestamp(sdf.parse(wzdTime).getTime()));
- if(BeanUtils.isNotEmpty(tranItemId)){
- tranItem.setId(tranItemId);
- }
- if(BeanUtils.isNotEmpty(tranCarId)){
- tranCar.setId(tranCarId);
- }
- tranCar.setTranOrders(tranOrders);
- tranOrders.setTranCar(tranCar);
- tranItem.setTranOrders(tranOrders);
- tranOrders.setTranItem(tranItem);
- if(BeanUtils.isNotEmpty(tranOrders.getId())){
- tranOrders.setUpdateBy(loginUserId);
- tranOrders.setUpdateDate(new Timestamp(System.currentTimeMillis()));
- tranOrdersManager.update4ClearCurrSession(tranOrders);
- }
- tranOrders.setCreateBy(loginUserId);
- tranOrders.setCreateDate(new Timestamp(System.currentTimeMillis()));
- tranOrdersManager.save(tranOrders);
- writerPrint("1");
- }else
- writerPrint("0");
- } catch (Exception e) {
- e.printStackTrace();
- writerPrint("2");
- }
- return NONE;
- }