首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

spring+hibernate+struts主从表保存出错!怎么解决

2011-12-27 
spring+hibernate+struts主从表保存出错!如何解决?主表orders(orderidint)从表orderitem(orderitemidint,o

spring+hibernate+struts主从表保存出错!如何解决?
主表orders(
orderid   int  
)
从表orderitem(
orderitemid   int,
orderid   int
)
数据库是oracle,主从表的关键字是自动增长的,从表设定了外键for_orderid,并设定对数据完整性条件。

实际运行情况:
java.sql.BatchUpdateException:   ORA-02291:   integrity   constraint   (SHA.FOR_ORDER_ID)   violated   -   parent   key   not   found
估计是保存从表的时候,主表没有提交。

我保存数据的时候在OrdersDAO的save方法中:
        public   void   save(Orders   transientInstance)   {
                log.debug( "saving   Orders   instance ");
                try   {
                        getHibernateTemplate().save(transientInstance);
                        Iterator   iterator   =   transientInstance.getOrderitems().iterator();
                        while   (iterator.hasNext()){
                        log.debug( "saving   Orderitem   instance ");
                        getHibernateTemplate().save((Orderitem)iterator.next());
                        }                      
                }   catch   (RuntimeException   re)   {
                        log.error( "save   failed ",   re);
                        throw   re;
                }
        }
我查看debug的时候,从表的外键已经被赋值了,并且等于主表orderid值
...
DEBUG   [http-8080-1]   (AbstractBatcher.java:324)   -   insert   into   SHA.ORDERS   (CUST_ID,   TOTAL_PRICE,   CUSTOMERNAME,   TELEPHONE,   ADDRESS,   POST,   EMAIL,   ORDER_ID)   values   (?,   ?,   ?,   ?,   ?,   ?,   ?,   ?)
DEBUG   [http-8080-1]   (AbstractBatcher.java:378)   -   preparing   statement
DEBUG   [http-8080-1]   (BasicEntityPersister.java:1612)   -   Dehydrating   entity:   [com.ascent.bean.Orders#31]
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '1 '   to   parameter:   1
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '200.8 '   to   parameter:   2
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   'shang '   to   parameter:   3
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '13523452345 '   to   parameter:   4
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   'nanno '   to   parameter:   5
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '234543 '   to   parameter:   6
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   'shang@sh.com '   to   parameter:   7


DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '31 '   to   parameter:   8
DEBUG   [http-8080-1]   (BatchingBatcher.java:27)   -   Adding   to   batch
DEBUG   [http-8080-1]   (BasicEntityPersister.java:1825)   -   Inserting   entity:   [com.ascent.bean.Orderitem#33]
DEBUG   [http-8080-1]   (BatchingBatcher.java:54)   -   Executing   batch   size:   1
DEBUG   [http-8080-1]   (BatchingBatcher.java:84)   -   success   of   batch   update   unknown:   0
DEBUG   [http-8080-1]   (AbstractBatcher.java:298)   -   about   to   close   PreparedStatement   (open   PreparedStatements:   1,   globally:   1)
DEBUG   [http-8080-1]   (AbstractBatcher.java:416)   -   closing   statement
DEBUG   [http-8080-1]   (AbstractBatcher.java:290)   -   about   to   open   PreparedStatement   (open   PreparedStatements:   0,   globally:   0)
DEBUG   [http-8080-1]   (AbstractBatcher.java:324)   -   insert   into   SHA.ORDERITEM   (ORDER_ID,   QUANTITY,   BOOKID,   ORDERITEM_ID)   values   (?,   ?,   ?,   ?)
DEBUG   [http-8080-1]   (AbstractBatcher.java:378)   -   preparing   statement
DEBUG   [http-8080-1]   (BasicEntityPersister.java:1612)   -   Dehydrating   entity:   [com.ascent.bean.Orderitem#33]
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '31 '   to   parameter:   1
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '2 '   to   parameter:   2
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '1 '   to   parameter:   3
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '33 '   to   parameter:   4
DEBUG   [http-8080-1]   (BatchingBatcher.java:27)   -   Adding   to   batch
DEBUG   [http-8080-1]   (BasicEntityPersister.java:1825)   -   Inserting   entity:   [com.ascent.bean.Orderitem#34]
DEBUG   [http-8080-1]   (AbstractBatcher.java:154)   -   reusing   prepared   statement
DEBUG   [http-8080-1]   (AbstractBatcher.java:324)   -   insert   into   SHA.ORDERITEM   (ORDER_ID,   QUANTITY,   BOOKID,   ORDERITEM_ID)   values   (?,   ?,   ?,   ?)
DEBUG   [http-8080-1]   (BasicEntityPersister.java:1612)   -   Dehydrating   entity:   [com.ascent.bean.Orderitem#34]
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '31 '   to   parameter:   1
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '1 '   to   parameter:   2
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '2 '   to   parameter:   3
DEBUG   [http-8080-1]   (NullableType.java:59)   -   binding   '34 '   to   parameter:   4
DEBUG   [http-8080-1]   (BatchingBatcher.java:27)   -   Adding   to   batch
DEBUG   [http-8080-1]   (BatchingBatcher.java:54)   -   Executing   batch   size:   2
DEBUG   [http-8080-1]   (AbstractBatcher.java:298)   -   about   to   close   PreparedStatement   (open   PreparedStatements:   1,   globally:   1)


DEBUG   [http-8080-1]   (AbstractBatcher.java:416)   -   closing   statement
DEBUG   [http-8080-1]   (JDBCExceptionReporter.java:63)   -   Could   not   execute   JDBC   batch   update   [insert   into   SHA.ORDERITEM   (ORDER_ID,   QUANTITY,   BOOKID,   ORDERITEM_ID)   values   (?,   ?,   ?,   ?)]
java.sql.BatchUpdateException:   ORA-02291:   integrity   constraint   (SHA.FOR_ORDER_ID)   violated   -   parent   key   not   found

为什么提交数据的时候提示错误?这时候主表在数据库中也没有保存?需要调整哪些配置?

[解决办法]

探讨
问题得到解决,奇怪的论坛,光看不复,非学习之道也。
原来是orderitem的外键orderid与主表的orderid类型不一致。

[解决办法]
哥们得细心点啊

热点排行