分类: Java
2009-08-17 16:47:03
Hibernate: select max(orders_id) from orders
Hibernate: insert into orders (member_id, orderno, realname, address, zip, tel, payment, email, memo, price, time, tag, orders_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select member0_.member_id as member1_0_, member0_.username as username0_, member0_.password as password0_, member0_.realname as realname0_, member0_.tel as tel0_, member0_.address as address0_, member0_.zip as zip0_, member0_.email as email0_ from member member0_ where member0_.username=?
Hibernate: select orders0_.member_id as member2_1_, orders0_.orders_id as orders1_1_, orders0_.orders_id as orders1_5_0_, orders0_.member_id as member2_5_0_, orders0_.orderno as orderno5_0_, orders0_.realname as realname5_0_, orders0_.address as address5_0_, orders0_.zip as zip5_0_, orders0_.tel as tel5_0_, orders0_.payment as payment5_0_, orders0_.email as email5_0_, orders0_.memo as memo5_0_, orders0_.price as price5_0_, orders0_.time as time5_0_, orders0_.tag as tag5_0_ from orders orders0_ where orders0_.member_id=?
1
Hibernate: update orders set member_id=null where member_id=?
SQL Error: 0, SQLState: 01004
Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'member_id' at row 1
Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at net.yywn.hibernate.database.DbOperate.delete(DbOperate.java:602)
at net.yywn.hibernate.database.DbOperateTest.testOneToMany(DbOperateTest.java:265)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'member_id' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 25 more
问题分析:
1(这是该用户的订单数)
Hibernate: update orders set member_id=null where member_id=?(开始执行delete该用户)
这是上面代码片段的分析:可知,该delete的操作没事执行,而是先去执行update。由于member_id为null,而
数据库中相应的字段设置不为null。
问题解决:
当然不能去该数据库的那个字段(设置为null,那就是缘木求鱼了)
细看配置文件,发现member没配inverse(初始值为false)属性。
把它设为true即可!
个人认为inverse属性是用来设置主动权的。在member的set(orders)中设置其为true,表示把主动权交给了
member,这样删除member的同时就把member的orders也删除了。(其实是先删orders再member)
成功后部分sql如下:
Hibernate: delete from orders where orders_id=?
Hibernate: delete from orders where orders_id=?
Hibernate: delete from member where member_id=?
sql如下:
Hibernate: delete from member where member_id=?
Hibernate: update orders set member_id = null
Hibernate: delete from orders where orders_id=?
Hibernate: delete from orders where orders_id=?