Results 1 to 2 of 2

Thread: Infinite select loop after call to persist()

  1. #1

    Default Infinite select loop after call to persist()

    I have the following classes:

    Code:
    @Configurable
    @Entity
    @Table(name = "MAIN_PACKAGE")
    public class MainPackage {
    
        @OneToOne(targetEntity = Submitter.class, mappedBy = "maingPackage")
        private Submitter submitter;
    
    }
    Code:
    @Configurable
    @Entity
    @Table(name = "SUBMITTER")
    public class Submitter {
    
        @OneToOne(targetEntity = MainPackage.class)
        @JoinColumn(name = "SUBMITTER_ID")
        private MainPackage mainPackage;
    
        @OneToOne(targetEntity = Company.class, mappedBy = "submitter")
        private Company company;
    
        @OneToOne(targetEntity = Address.class, mappedBy = "submitter")
        private Address address;
    }
    Code:
    @Configurable
    @Entity
    @Table(name = "COMPANY")
    public class Company {
        
        @OneToOne(targetEntity = Submitter.class)
        @JoinColumn(name = "CO_ID")
        private Submitter submitter;
    }
    Code:
    @Configurable
    @Entity
    @Table(name = "ADDRESS")
    public class Address {
    	
        @OneToOne(targetEntity = Submitter.class)
        @JoinColumn(name = "ADD_ID")
        private Submitter submitter;
    }
    Problem: After a call to persist() on the MainPackage object, Hibernate calls select on the Submitter table in an infinite loop, which eventually generates an OutOfMemoryError:

    select submitter0_.submitter_id as submitter1_10_4_...
    select submitter0_.submitter_id as submitter1_10_4_...
    select submitter0_.submitter_id as submitter1_10_4_...
    select submitter0_.submitter_id as submitter1_10_4_...
    select submitter0_.submitter_id as submitter1_10_4_...

    Is there a circular reference hidden somewhere in the above code? Here is how I basically am calling persist():

    Code:
    MainPackage package = MainPackage.findPackage(id);
    package.setDescription(formBean.getDescription());
    Submitter submitter = formBean.getSubmitter();
    submitter.setName(formBean.getSubmitter().getName());
    .
    .
    package.persist();

  2. #2

    Default

    The solution that I found until someone corrects me:

    Replace

    Code:
    @Configurable
    @Entity
    @Table(name = "MAIN_PACKAGE")
    public class MainPackage {
    
        @OneToOne(targetEntity = Submitter.class, mappedBy = "maingPackage")
        private Submitter submitter;
    
    }
    with

    Code:
    @Configurable
    @Entity
    @Table(name = "MAIN_PACKAGE")
    public class MainPackage {
    
        @Column(name = "SUBMITTER_ID")
        private Integer submitterId;
    
    }
    When it's time to persist on the MainPackage object, retrieve the Submitter object by separate query using the submitterId, set values on the MainPackage and Submitter objects separately, then do this:

    Code:
    submitter.persist();
    mainPackage.persist();
    Probably not an ideal JPA/Hibernate solution, but it works and I don't get the infinite loop of selects on the submitter table anymore.

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
  •