Hibernate, one to second relationship: Data truncation: column was set to data type implicit default; null Suppl

Source: Internet
Author: User
Tags sql error

Problem description: My example is membership and order

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 _, ORD Ers0 _. 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 and sqlstate: 01004
Data truncation: column was set to data type implicit default; null supplied for not null column 'Member _ id' at Row 1
Cocould not synchronize database state with Session
Org. hibernate. Exception. genericjdbcexception: cocould 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.exe cutebatch (abstractbatcher. Java: 202)
At org.hibernate.engine.actionqueue.exe cuteactions (actionqueue. Java: 235)
At org.hibernate.engine.actionqueue.exe cuteactions (actionqueue. Java: 141)
At org. hibernate. event. Def. abstractflushingeventlistener. extends mexecutions (abstractflushingeventlistener. Java: 297)
At org. hibernate. event. Def. defaultflusheventlistener. onflush (defaflusheventlistener. 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. testonetotify (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.exe cutebatch (serverpreparedstatement. Java: 657)
At org. hibernate. JDBC. batchingbatcher. doexecutebatch (batchingbatcher. Java: 58)
At org.hibernate.jdbc.abstractbatcher.exe cutebatch (abstractbatcher. Java: 195)
... 25 more
 

Problem Analysis:

1 (this is the user's order number)
Hibernate: Update orders set member_id = NULL where member_id =? (Start executing Delete this user)

This is the analysis of the code snippet above: we can see that the delete operation is not executed, but the update operation is executed first. Because member_id is null

The corresponding fields in the database are not set to null.

Solution:

Of course, you cannot go to the field of the database (set to null, that is, Yuan Mu Qiuyu)

Looking at the configuration file, we found that member is not configured with the inverse (Initial Value: false) attribute.

Set it to true!

I personally think that the inverse attribute is used to set the initiative. Set it to true in the Set (orders) of member, indicating that the initiative is handed over

In this way, the member orders will also be deleted when the member is deleted. (Delete orders before Member)

Some SQL statements are as follows:

Hibernate: delete from orders where orders_id =?
Hibernate: delete from orders where orders_id =?
Hibernate: delete from member where member_id =?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.