Java Spring和Hibernate无限循环保存数据

时间:2016-02-08 22:19:55

标签: java spring hibernate

我正在开发具有驱动程序和许可证的Java Spring应用程序。在MySQL数据库中,表驱动程序和许可证通过表DRIVER_LICENSE连接到多个。 DRIVER_LICENSE具有由driverID和licenseID(它们是整数)组成的复合键(复合键)。此外,DRIVER_LICENSE还有其他字段,如expiration_date和state_issued。为了处理我使用过的其他字段:

在连接表示例中使用额外列来休眠多对多关联 http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example

我的模型层的组织方式与该示例类似。我已经在两个One To Many连接中制作了Many To Many连接,就像那个例子一样,并且一切都很好。现在,当我保存驱动程序数据时,存在无限循环。我的驱动程序在类Driver中有List列表,属于Model层。当我 保存驱动程序在那个地方有一个无限循环。

这是该循环的示例: ```

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

还有一件事。当谈到无限循环时,我有很多GSON错误。

无限循环有没有解决这个问题的方法?

1 个答案:

答案 0 :(得分:0)

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 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.