Results 1 to 8 of 8
  1. #1
    MStrahinic is offline Member
    Join Date
    Jan 2016
    Posts
    4
    Rep Power
    0

    Default Java Spring and Hibernate infinite loop problem 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
    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<DriverLicense> 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.

    Is there any solution to this problem with infinite loops?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    Just want to check your model...so a Driver can have multiple Licenses?
    A License can have multiple Drivers?
    So what is a License?

    The model sounds a little wrong to me.

    Anyway, your GSON errors sound like you haven't given GSON a cutoff point.
    I think there's an annotation that tells it what to ignore.
    So for a DriverLicense you might want to ignore the driver attribute so it doesn't get into the infinite loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    MStrahinic is offline Member
    Join Date
    Jan 2016
    Posts
    4
    Rep Power
    0

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    Quote Originally Posted by Tolls View Post
    Just want to check your model...so a Driver can have multiple Licenses?
    A License can have multiple Drivers?
    So what is a License?

    The model sounds a little wrong to me.

    Anyway, your GSON errors sound like you haven't given GSON a cutoff point.
    I think there's an annotation that tells it what to ignore.
    So for a DriverLicense you might want to ignore the driver attribute so it doesn't get into the infinite loop.

    In this application I have panel to add new Drivers. On that panel there is Licenses section. There are several types of licenses depending on cargo that driver can transport. Special license is for example if he can transport hazard material or not. So one driver can have more licenses. One license can be attached to more drivers. Table that connects them, DRIVER_LICENSE, has composite key that consists of driverID and licenseID, and has additional fields. That additional fields are license expiration date, and state in which license was issued. Here is my example with Driver, License, and DriverLicense classes with DriverLicenseID class that represents composite id.

    This is driver class:

    @Entity
    @Table(name="Driver")
    public class Driver {

    @Id
    @GeneratedValue
    @Column(name = "driverID")
    private Integer driverID;

    private String firstName;
    private String lastName;
    private String middleInitials;

    @DateTimeFormat(pattern = "MM/dd/yyyy")
    private Date dateOfBirth;

    /////////////////////////////////////get and set///////////////////////////////

    public Integer getDriverID() {
    return driverID;
    }

    public void setDriverID(Integer driverID) {
    this.driverID = driverID;
    }

    public String getFirstName() {
    return firstName;
    }

    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }

    public String getLastName() {
    return lastName;
    }

    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    public String getMiddleInitials() {
    return middleInitials;
    }

    public void setMiddleInitials(String middleInitials) {
    this.middleInitials = middleInitials;
    }

    public Date getDateOfBirth() {
    return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
    }

    /////////////////////////////// one to many //////////////////////////////////

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "id.driver", cascade = CascadeType.ALL)
    public List<DriverLicense> driverLicense = new ArrayList<DriverLicense>();

    public List<DriverLicense> getDriverLicense() {
    return driverLicense;
    }
    public void setDriverLicense(List<DriverLicense> driverLicense) {
    this.driverLicense = driverLicense;
    }

    }



    This is License class:

    @Entity
    @Table(name="License")
    public class License {

    @Id
    @GeneratedValue
    @Column(name = "licenseID")
    private Integer licenseID;

    @Column(name = "licenseName")
    private String licenseName;


    public Integer getLicenseID() {
    return licenseID;
    }
    public void setLicenseID(Integer licenseID) {
    this.licenseID = licenseID;
    }
    public String getLicenseName() {
    return licenseName;
    }
    public void setLicenseName(String licenseName) {
    this.licenseName = licenseName;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "id.license", cascade = CascadeType.PERSIST)
    private List<DriverLicense> driverLicense = new ArrayList<DriverLicense>();

    public List<DriverLicense> getDriverLicense() {
    return driverLicense;
    }
    public void setDriverLicense(List<DriverLicense> driverLicense) {
    this.driverLicense = driverLicense;
    }

    }


    Here is DriverLicense class:

    @Entity
    @Table(name="DRIVER_LICENSE")
    @AssociationOverrides({
    @AssociationOverride(name = "id.driver",
    joinColumns = @JoinColumn(name = "driverID")),
    @AssociationOverride(name = "id.license",
    joinColumns = @JoinColumn(name = "licenseID")) })
    public class DriverLicense {

    // composite-id key
    @EmbeddedId
    private DriverLicenseID id = new DriverLicenseID();

    //additional fields
    @Column(name = "expirationDate")
    private Date expirationDate;

    @Column(name = "stateIssued")
    private String stateIssued;

    public DriverLicenseID getId() {
    return id;
    }

    public void setId(DriverLicenseID id) {
    this.id = id;
    }

    public Driver getDriver() {
    return getId().getDriver();
    }

    public void setDriver(Driver driver) {
    getId().setDriver(driver);
    }

    public License getLicense() {
    return getId().getLicense();
    }

    public void setLicense(License license) {
    getId().setLicense(license);
    }

    public String getStateIssued() {
    return stateIssued;
    }

    public void setStateIssued(String stateIssued) {
    this.stateIssued = stateIssued;
    }

    public Date getExpirationDate() {
    return expirationDate;
    }

    public void setExpirationDate(Date expirationDate) {
    this.expirationDate = expirationDate;
    }
    }


    Here is DriverLicenseID:

    @Embeddable
    public class DriverLicenseID implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManyToOne
    private Driver driver;

    public Driver getDriver() {
    return driver;
    }

    public void setDriver(Driver driver) {
    this.driver = driver;
    }

    @ManyToOne
    private License license;

    public License getLicense() {
    return license;
    }

    public void setLicense(License license) {
    this.license = license;
    }

    }


    Also, I have tried several GSON anotations like @Expose, but none of them is working. Maybe I am doing something wrong.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    Well, looking through the GSON threads on things like StackOverflow, it does seem to be a problem with GSON (unless there's something newer).

    Could you shift to Jackson? That's what I've used.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    MStrahinic is offline Member
    Join Date
    Jan 2016
    Posts
    4
    Rep Power
    0

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    With Jackson I remember that I had some error 415 and 500.
    I have examined my code, and there is one paradigm.

    {
    "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"


    Composite key that I have consists of Driver and License objects. When I click submit to save driver I managed to save it successfully, and from 3 drivers licenses submitted I have saved last in the list.

    These infinite loop I have, seams normal since I'm saving driver with DriverLicense list. First element of DriverLicense is composite key, made from driver and license objects. When I save driver it is printed in a loop since DriverLicense has driver again in primary key. I had some problems to Excude data from GSON. Now I'm not sure if this is just GSON problem. Maybe there is a problem with my logic or my Hibernate part of application, used to save data.
    Last edited by MStrahinic; 02-10-2016 at 07:28 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    I think, as suggested over at Code Ranch, the usual route is using a DTO that then allows you to control the data sent across.
    Sending your Hibernate object directly exposes your database structure to calling code, which can cause maintenance issues. Adding a column to your Hibernate objects means your JSON interface has also changed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    MStrahinic is offline Member
    Join Date
    Jan 2016
    Posts
    4
    Rep Power
    0

    Default Re: Java Spring and Hibernate infinite loop problem on save data

    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: Chapter*13.*Batch processing
    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 license data into database. First I save driver data, and then I'm calling saveAll to 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 your help. It really was GSON problem with serialization.
    Last edited by MStrahinic; 02-12-2016 at 09:21 PM.

Similar Threads

  1. Hibernate Spring Struts Integration Problem
    By nayal_java in forum Hibernate
    Replies: 2
    Last Post: 11-19-2013, 01:02 PM
  2. Replies: 0
    Last Post: 01-12-2013, 05:36 AM
  3. infinite loop problem
    By JohnPringle83 in forum New To Java
    Replies: 10
    Last Post: 05-11-2011, 09:24 PM
  4. java recursion infinite loop
    By tony404 in forum Advanced Java
    Replies: 9
    Last Post: 10-03-2008, 01:16 PM
  5. Log4J, problem with Hibernate and Spring
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 03:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •