java ee - Hibernate one to many:"many" side record's foreign key is updated to null automatically -


i defined 2 entity following 1.booking

@entity @table(name="booking") public class booking extends basemodel{     ..     @onetomany(cascade=cascadetype.all,fetch=fetchtype.lazy)     @joincolumn(name="bookingno")     private list<part>parts = new arraylist<part>();   ... 

2.part

@entity @table(name="part") public class part extends basemodel{     @id     @generatedvalue     private integer sequence;     @column(length=50)     private string bookingno; 

now issue @ first saved 1 booking 2 part,then in view(jsp),i update booking , added new part ,finally save booking again new part create @ second time.if okay,i think in db,it should have 3 part,yes,it have 3 part in db,two saved @ first time , third part saved @ second time.but make me surprised first 2 part's foreign key "bookingno" updated null. using hibernate's saveorupdate api.is because of when save booking second time,there 1 part under booking,such hibernate regard others parts(the 2 part saved @ first time) deleted default? please see log ,it has below log: update part set bookingno=null bookingno=? when save booking

21:41:02,476 debug bookingdao:65 - save(),get session , start save booking 21:41:02,478 debug versionvalue:44 - version unsaved-value strategy undefined 21:41:02,479 debug identifiervalue:77 - id unsaved-value strategy undefined 21:41:02,480 debug abstractentitypersister:1004 - getting current persistent state for: [com.chailie.booking.model.booking.booking#samsung-100002] 21:41:02,482 debug abstractbatcher:358 - open preparedstatement (open preparedstatements: 0, globally: 0) 21:41:02,483 debug sql:393 - select booking_.bookingno, booking_.bookedby bookedby0_, booking_.bookingdate bookingd3_0_, booking_.businessservice business4_0_, booking_.cargotype cargotype0_, booking_.consignee consignee0_, booking_.consigneeaddress consigne7_0_, booking_.consigneecity consigne8_0_, booking_.consigneecountry consigne9_0_, booking_.consigneename consign10_0_, booking_.consigneepostal consign11_0_, booking_.consigneestate consign12_0_, booking_.createdate createdate0_, booking_.customer customer0_, booking_.customeraddress custome15_0_, booking_.customercity custome16_0_, booking_.customercountry custome17_0_, booking_.customername custome18_0_, booking_.customerpostal custome19_0_, booking_.customerstate custome20_0_, booking_.frtpayterm frtpayterm0_, booking_.incoterms incoterms0_, booking_.otherpayterm otherpa23_0_, booking_.remark remark0_, booking_.servicelevel service25_0_, booking_.shipper shipper0_, booking_.shipperaddress shipper27_0_, booking_.shippercity shipper28_0_, booking_.shippercountry shipper29_0_, booking_.shippername shipper30_0_, booking_.shipperpostal shipper31_0_, booking_.shipperstate shipper32_0_, booking_.status status0_, booking_.timestamp timestamp0_ booking booking_ booking_.bookingno=? 21:41:02,487 debug abstractbatcher:476 - preparing statement 21:41:02,489 debug stringtype:80 - binding 'samsung-100002' parameter: 1 21:41:02,492 debug stringtype:122 - returning 'chailieyang' column: bookedby0_ 21:41:02,494 debug timestamptype:122 - returning '2013-06-06 00:00:00' column: bookingd3_0_ 21:41:02,496 debug stringtype:122 - returning 'sea' column: business4_0_ 21:41:02,497 debug stringtype:122 - returning '' column: cargotype0_ 21:41:02,498 debug stringtype:122 - returning 'lenovo' column: consignee0_ 21:41:02,500 debug stringtype:122 - returning '' column: consigne7_0_ 21:41:02,501 debug stringtype:122 - returning '' column: consigne8_0_ 21:41:02,502 debug stringtype:122 - returning 'usa' column: consigne9_0_ 21:41:02,503 debug stringtype:122 - returning 'zhao yang,bei jing china' column: consign10_0_ 21:41:02,504 debug stringtype:122 - returning '' column: consign11_0_ 21:41:02,505 debug stringtype:122 - returning 'alabama' column: consign12_0_ 21:41:02,506 debug timestamptype:122 - returning '2013-05-02 21:39:03' column: createdate0_ 21:41:02,507 debug stringtype:122 - returning 'samsung' column: customer0_ 21:41:02,508 debug stringtype:122 - returning '' column: custome15_0_ 21:41:02,509 debug stringtype:122 - returning '' column: custome16_0_ 21:41:02,510 debug stringtype:122 - returning 'usa' column: custome17_0_ 21:41:02,511 debug stringtype:122 - returning 'samsung co,korea' column: custome18_0_ 21:41:02,512 debug stringtype:122 - returning '' column: custome19_0_ 21:41:02,514 debug stringtype:122 - returning 'alabama' column: custome20_0_ 21:41:02,514 debug stringtype:122 - returning '' column: frtpayterm0_ 21:41:02,515 debug stringtype:122 - returning '' column: incoterms0_ 21:41:02,516 debug stringtype:122 - returning '' column: otherpa23_0_ 21:41:02,517 debug stringtype:122 - returning '' column: remark0_ 21:41:02,518 debug stringtype:122 - returning '' column: service25_0_ 21:41:02,519 debug stringtype:122 - returning 'samsung' column: shipper0_ 21:41:02,520 debug stringtype:122 - returning '' column: shipper27_0_ 21:41:02,521 debug stringtype:122 - returning '' column: shipper28_0_ 21:41:02,522 debug stringtype:122 - returning 'usa' column: shipper29_0_ 21:41:02,524 debug stringtype:122 - returning 'samsung co,korea' column: shipper30_0_ 21:41:02,527 debug stringtype:122 - returning '' column: shipper31_0_ 21:41:02,528 debug stringtype:122 - returning 'alabama' column: shipper32_0_ 21:41:02,531 debug stringtype:122 - returning 'n' column: status0_ 21:41:02,532 debug timestamptype:122 - returning '2013-05-02 21:39:49' column: timestamp0_ 21:41:02,533 debug abstractbatcher:366 - close preparedstatement (open preparedstatements: 1, globally: 1) 21:41:02,534 debug abstractbatcher:525 - closing statement 21:41:02,535 debug abstractsaveeventlistener:498 - detached instance of: com.chailie.booking.model.booking.booking 21:41:02,536 debug defaultsaveorupdateeventlistener:203 - updating detached instance 21:41:02,537 debug defaultsaveorupdateeventlistener:249 - updating [com.chailie.booking.model.booking.booking#samsung-100002] 21:41:02,538 debug reattachvisitor:60 - collection dereferenced while transient [com.chailie.booking.model.booking.booking.parts#samsung-100002] 21:41:02,539 debug reattachvisitor:60 - collection dereferenced while transient [com.chailie.booking.model.booking.booking.todoitems#samsung-100002] 21:41:02,540 debug defaultsaveorupdateeventlistener:298 - updating [com.chailie.booking.model.booking.booking#samsung-100002] 21:41:02,541 debug cascade:115 - processing cascade action_save_update for: com.chailie.booking.model.booking.booking 21:41:02,542 debug cascade:291 - cascade action_save_update collection: com.chailie.booking.model.booking.booking.parts 21:41:02,543 debug cascadingaction:216 - cascading saveorupdate: com.chailie.booking.model.booking.part 21:41:02,545 debug abstractsaveeventlistener:489 - transient instance of: com.chailie.booking.model.booking.part 21:41:02,546 debug defaultsaveorupdateeventlistener:161 - saving transient instance 21:41:02,547 debug abstractsaveeventlistener:152 - saving [com.chailie.booking.model.booking.part#<null>] 21:41:02,548 debug abstractsaveeventlistener:240 - executing insertions 21:41:02,550 debug versioning:42 - seeding: 2013-05-02 21:41:02.549 21:41:02,552 debug abstractsaveeventlistener:289 - executing identity-insert 21:41:02,555 debug abstractentitypersister:2094 - inserting entity: com.chailie.booking.model.booking.part (native id) 21:41:02,557 debug abstractentitypersister:2096 - version: 2013-05-02 21:41:02.549 21:41:02,559 debug abstractbatcher:358 - open preparedstatement (open preparedstatements: 0, globally: 0) 21:41:02,563 debug sql:393 - insert part (bookingno, createdate, partno, pono, qtyreceived, qtyshipped, qtyunit, timestamp) values (?, ?, ?, ?, ?, ?, ?, ?) 21:41:02,566 debug abstractbatcher:476 - preparing statement 21:41:02,570 debug abstractentitypersister:1942 - dehydrating entity: [com.chailie.booking.model.booking.part#<null>] 21:41:02,572 debug stringtype:80 - binding 'samsung-100002' parameter: 1 21:41:02,574 debug timestamptype:73 - binding null parameter: 2 21:41:02,577 debug stringtype:80 - binding '' parameter: 3 21:41:02,579 debug stringtype:80 - binding '9999' parameter: 4 21:41:02,587 debug integertype:73 - binding null parameter: 5 21:41:02,590 debug integertype:73 - binding null parameter: 6 21:41:02,591 debug stringtype:80 - binding '' parameter: 7 21:41:02,595 debug timestamptype:80 - binding '2013-05-02 21:41:02' parameter: 8 21:41:02,598 debug identifiergeneratorfactory:37 - natively generated identity: 4 21:41:02,600 debug abstractbatcher:366 - close preparedstatement (open preparedstatements: 1, globally: 1) 21:41:02,602 debug abstractbatcher:525 - closing statement 21:41:02,605 debug cascade:306 - done cascade action_save_update collection: com.chailie.booking.model.booking.booking.parts 21:41:02,606 debug cascade:150 - done processing cascade action_save_update for: com.chailie.booking.model.booking.booking 21:41:02,608 debug bookingdao:68 - after save booking,the booking [com.chailie.booking.model.booking.booking@5774a1e3] 21:41:02,611 debug jdbctransaction:103 - commit 21:41:02,612 debug sessionimpl:337 - automatically flushing session 21:41:02,614 debug abstractflushingeventlistener:58 - flushing session 21:41:02,615 debug abstractflushingeventlistener:111 - processing flush-time cascades 21:41:02,616 debug cascade:115 - processing cascade action_save_update for: com.chailie.booking.model.booking.booking 21:41:02,618 debug cascade:291 - cascade action_save_update collection: com.chailie.booking.model.booking.booking.parts 21:41:02,619 debug cascadingaction:216 - cascading saveorupdate: com.chailie.booking.model.booking.part 21:41:02,620 debug abstractsaveeventlistener:463 - persistent instance of: com.chailie.booking.model.booking.part 21:41:02,621 debug defaultsaveorupdateeventlistener:105 - ignoring persistent instance 21:41:02,623 debug defaultsaveorupdateeventlistener:142 - object associated session: [com.chailie.booking.model.booking.part#4] 21:41:02,625 debug cascade:306 - done cascade action_save_update collection: com.chailie.booking.model.booking.booking.parts 21:41:02,626 debug cascade:150 - done processing cascade action_save_update for: com.chailie.booking.model.booking.booking 21:41:02,627 debug abstractflushingeventlistener:154 - dirty checking collections 21:41:02,628 debug abstractflushingeventlistener:171 - flushing entities , processing referenced collections 21:41:02,630 debug wrapvisitor:87 - wrapped collection in role: com.chailie.booking.model.booking.booking.parts 21:41:02,631 debug collections:176 - collection found: [com.chailie.booking.model.booking.booking.parts#samsung-100002], was: [<unreferenced>] (initialized) 21:41:02,633 debug abstractflushingeventlistener:210 - processing unreferenced collections 21:41:02,635 debug abstractflushingeventlistener:224 - scheduling collection removes/(re)creates/updates 21:41:02,636 debug abstractflushingeventlistener:85 - flushed: 0 insertions, 0 updates, 0 deletions 2 objects 21:41:02,637 debug abstractflushingeventlistener:91 - flushed: 1 (re)creations, 0 updates, 2 removals 1 collections 21:41:02,638 debug printer:83 - listing entities: 21:41:02,639 debug printer:90 - com.chailie.booking.model.booking.part{timestamp=2013-05-02 21:41:02, pono=9999, qtyshipped=null, partno=, sequence=4, bookingno=samsung-100002, qtyreceived=null, createdate=null, qtyunit=} 21:41:02,641 debug printer:90 - com.chailie.booking.model.booking.booking{remark=, consignee=lenovo, customercity=, customerstate=alabama, parts=[com.chailie.booking.model.booking.part#4], servicelevel=, customer=samsung, otherpayterm=, consigneecity=, consigneepostal=, timestamp=2013-05-02 21:39:49, businessservice=sea, shippername=samsung co,korea, customercountry=usa, cargotype=, shippercity=, createdate=2013-05-02 21:39:03, customerpostal=, shipperpostal=, customername=samsung co,korea, consigneename=zhao yang,bei jing china, bookingdate=2013-06-06 00:00:00, consigneeaddress=, incoterms=, status=n, shippercountry=usa, shipper=samsung, bookingno=samsung-100002, shipperstate=alabama, shipperaddress=, todoitems=null, consigneecountry=usa, customeraddress=, frtpayterm=, consigneestate=alabama, bookedby=chailieyang} 21:41:02,644 debug abstractflushingeventlistener:290 - executing flush 21:41:02,646 debug connectionmanager:463 - registering flush begin 21:41:02,647 debug abstractcollectionpersister:1010 - deleting collection: [com.chailie.booking.model.booking.booking.parts#samsung-100002] 21:41:02,648 debug abstractbatcher:358 - open preparedstatement (open preparedstatements: 0, globally: 0) 21:41:02,650 debug sql:393 - update part set bookingno=null bookingno=? 21:41:02,651 debug abstractbatcher:476 - preparing statement 21:41:02,653 debug stringtype:80 - binding 'samsung-100002' parameter: 1 21:41:02,655 debug abstractcollectionpersister:1067 - done deleting collection 21:41:02,656 debug abstractcollectionpersister:1010 - deleting collection: [com.chailie.booking.model.booking.booking.todoitems#samsung-100002] 21:41:02,657 debug abstractbatcher:44 - executing batch size: 1 21:41:02,660 debug abstractbatcher:366 - close preparedstatement (open preparedstatements: 1, globally: 1) 21:41:02,661 debug abstractbatcher:525 - closing statement 21:41:02,663 debug abstractbatcher:358 - open preparedstatement (open preparedstatements: 0, globally: 0) 21:41:02,664 debug sql:393 - update todoitem set bookingno=null bookingno=? 21:41:02,666 debug abstractbatcher:476 - preparing statement 21:41:02,668 debug stringtype:80 - binding 'samsung-100002' parameter: 1 21:41:02,669 debug abstractcollectionpersister:1067 - done deleting collection 21:41:02,670 debug abstractbatcher:44 - executing batch size: 1 21:41:02,672 debug abstractbatcher:366 - close preparedstatement (open preparedstatements: 1, globally: 1) 21:41:02,673 debug abstractbatcher:525 - closing statement 21:41:02,675 debug abstractcollectionpersister:1090 - inserting collection: [com.chailie.booking.model.booking.booking.parts#samsung-100002] 21:41:02,677 debug abstractbatcher:358 - open preparedstatement (open preparedstatements: 0, globally: 0) 21:41:02,679 debug sql:393 - update part set bookingno=? sequence=? 21:41:02,682 debug abstractbatcher:476 - preparing statement 21:41:02,685 debug stringtype:80 - binding 'samsung-100002' parameter: 1 21:41:02,687 debug integertype:80 - binding '4' parameter: 2 21:41:02,688 debug abstractcollectionpersister:1172 - done inserting collection: 1 rows inserted 21:41:02,689 debug abstractbatcher:44 - executing batch size: 1 21:41:02,692 debug abstractbatcher:366 - close preparedstatement (open preparedstatements: 1, globally: 1) 21:41:02,694 debug abstractbatcher:525 - closing statement 21:41:02,695 debug connectionmanager:472 - registering flush end 21:41:02,696 debug abstractflushingeventlistener:321 - post flush 21:41:02,697 debug jdbccontext:201 - before transaction completion 21:41:02,698 debug sessionimpl:393 - before transaction completion 21:41:02,889 debug jdbctransaction:193 - re-enabling autocommit 21:41:02,891 debug jdbctransaction:116 - committed jdbc connection 21:41:02,893 debug jdbccontext:215 - after transaction completion 21:41:02,895 debug connectionmanager:296 - transaction completed on session on_close connection release mode; sure close session release jdbc resources! 21:41:02,896 debug sessionimpl:422 - after transaction completion 21:41:02,897 debug sessionimpl:353 - automatically closing session 21:41:02,898 debug sessionimpl:273 - closing session 21:41:02,899 debug connectionmanager:374 - performing cleanup 21:41:02,900 debug connectionmanager:435 - releasing jdbc connection [ (open preparedstatements: 0, globally: 0) (open resultsets: 0, globally: 0)] 21:41:02,901 debug jdbccontext:215 - after transaction completion 21:41:02,902 debug connectionmanager:296 - transaction completed on session on_close connection release mode; sure close session release jdbc resources! 21:41:02,904 debug sessionimpl:422 - after transaction completion 21:41:02,905 debug sessionimpl:273 - closing session 21:41:02,906 debug bookingutil:298 - initbooking() start booking [com.chailie.booking.model.booking.booking@5774a1e3],user [user [username=chailieyang, loginip=0:0:0:0:0:0:0:1]] 21:41:02,907 debug bookingutil:111 - inittodoitems start todoitems [null] 21:41:02,918 debug bookingutil:75 - inittodoitems() start,init complete todoitem 21:41:02,922 debug servletdispatcherresult:68 - forwarding location newbooking.jsp 

i think problem caused set part's fk nullable before,after specify fk not nullable,it won't have problem anymore,please see part entity

@entity @table(name="part") public class part extends basemodel{     @id     @generatedvalue     private integer sequence;     @column(length=50,nullable=false,insertable=false,updatable=false) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -