Milan Strahinic February 2016

Java Spring and Hibernate infinite loop on save data

I am developing Java Spring application that has Drivers and Licenses. In MySQL database, tables Driver and License are connected Many To Many by table DRIVER_LICENSE. DRIVER_LICENSE has composite key(compound key) made from driverID and licenseID (which are integers). Also, DRIVER_LICENSE has additional fields like expiration_date and state_issued. In order to handle additional fields I have used this:

Hibernate Many-to-Many Association with Extra 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 is organized like on that example. I have braked Many To Many connection in two One To Many connections like on that example, and all works nice. Now when I save driver data there is infinite loop. My driver has List list in class Driver, that belongs to Model layer. When I save driver there is an infinite loop on that place.

This is the example of that 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 is one more thing. When it comes to infinite loop I have a lot of GSON errors.

Answers


Milan Strahinic February 2016

Thanks. I have managed to figure this out.

As you see, the problem is that I'm saving Driver, who has children (Fleet and License). In database, table Driver is connected with License by table DRIVER_LICENSE, which has composite ID made from driverID and licenseID. When saving data I wanted to save driver, fleet and license at same time. The loop you see is from saving driver who has license which has, in it self, driver, and that driver has license and this is infinite.

Here is how I solve this: I have used transient to avoid GSON problem with 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 is excluded from serialization but there remains problem of saving driverLicense data. To figure this out I have made saveAll function i driverLicenseDAO.

//saveAll batch insert for 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 i = 0;
        for ( DriverLicense item : driverLicenseList) {
            i++;
            session.save(item);
            if ( i % 20 == 0 ) { //20, same as the JDBC batch size
                //flush a batch of inserts and release memory:
                session.flush();
                session.clear();
            }
        }

        tx.commit();
        session.close();
    }

I use this saveAll() function to save driver licens

Post Status

Asked in February 2016
Viewed 2,824 times
Voted 10
Answered 1 times

Search




Leave an answer