Java Spring and Hibernate infinite loop on save data -
i developing java spring application has drivers , licenses. in mysql database, tables driver , license connected many many table driver_license. driver_license has composite key(compound key) made driverid , licenseid (which integers). also, driver_license has additional fields expiration_date , state_issued. in order handle additional fields have used this:
hibernate many-to-many association columns in join table example http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example
my model layer organized on example. have braked many many connection in 2 1 many connections on example, , works nice. when save driver data there infinite loop. driver has list list in class driver, belongs model layer. when save driver there infinite loop on place.
this example of loop: ```
{ "driverid":116, "firstname":"dd", "lastname":"dd", "middleinitials":"", "dateofbirth":"feb 9, 2016 12:00:00 am", "driverlicense":[ {"id": { "driver": { "driverid":116, "firstname":"dd", "lastname":"dd", "middleinitials":"", "dateofbirth":"feb 9, 2016 12:00:00 am", "driverlicense":[ {"id":{ "driver": { "driverid":116, "firstname":"dd", "lastname":"dd", "middleinitials":"", "dateofbirth":"feb 9, 2016 12:00:00 am" there 1 more thing. when comes infinite loop have lot of gson errors.
is there solution problem infinite loops?
thanks. have managed figure out.
as see, problem i'm saving driver, has children (fleet , license). in database, table driver connected license table driver_license, has composite id made driverid , licenseid. when saving data wanted save driver, fleet , license @ same time. loop see saving driver has license has, in self, driver, , driver has license , infinite.
here how solve this: have used transient avoid gson problem data serialising.
@onetomany(fetch = fetchtype.lazy, mappedby = "id.driver", cascade = cascadetype.persist) private transient list<driverlicense> driverlicense = new arraylist<driverlicense>(); public list<driverlicense> getdriverlicense() { return driverlicense; } public void setdriverlicense(list<driverlicense> driverlicense) { this.driverlicense = driverlicense; } now, driver license excluded serialization there remains problem of saving driverlicense data. figure out have made saveall function driverlicensedao.
//saveall batch insert hibernate: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html public void saveall(list<driverlicense> driverlicenselist) throws exception{ session session = sessionfactory.opensession(); transaction tx = session.begintransaction(); int = 0; ( driverlicense item : driverlicenselist) { i++; session.save(item); if ( % 20 == 0 ) { //20, same jdbc batch size //flush batch of inserts , release memory: session.flush(); session.clear(); } } tx.commit(); session.close(); } i use saveall() function save driver license data database. first save driver data, , i'm calling saveall save driverlicense data.
public void adddriver(driver driver, list<fleet> fleetlist, list<driverlicense> driverlicenselist ) throws exception { //fleet driver.setfleet(fleetlist); //save driver getcurrentsession().save(driver); //save driverlicense driverlicensedao.saveall(driverlicenselist); } thanks on help. gson problem serialization.
Comments
Post a Comment