繁体   English   中英

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

[英]Java Spring and Hibernate infinite loop on save data

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

联接表示例中带有额外列的Hibernate多对多关联http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-例

我的模型层的组织方式类似于该示例。 我已经在两个“一对多”连接中制动了该示例中的“多对多”连接,并且一切正常。 现在,当我保存驱动程序数据时,就会出现无限循环。 我的驱动程序在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错误。

无限循环可以解决这个问题吗?

谢谢。 我设法弄清楚了。

如您所见,问题在于我要保存有孩子(车队和许可证)的Driver。 在数据库中,表Driver通过表DRIVER_LICENSE与License连接,该表具有由driverID和licenseID组成的复合ID。 保存数据时,我想同时保存驱动程序,车队和许可证。 您看到的循环来自保存具有许可证的驱动程序,该驱动程序本身具有驱动程序,并且该驱动程序具有许可证,并且这是无限的。

这是我解决此问题的方法:我已使用瞬态来避免数据序列化的GSON问题。

@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;
     }

现在,从序列化中排除了驱动程序许可证,但是仍然存在保存driverLicense数据的问题。 为了弄清楚这一点,我在driverLicenseDAO中做了saveAll函数。

//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();
    }

我使用这个saveAll()函数将驾驶执照数据保存到数据库中。 首先,我保存驱动程序数据,然后调用saveAll来保存driverLicense数据。

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);
    }

谢谢您的帮助。 序列化确实是GSON问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM