infinite loop while fetching data using hibernate

主宰稳场 提交于 2019-12-24 17:27:35

问题


i am fetching data using HQl but one strange exception occurring while querying please look at this once

at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4107)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:680)
    at org.hibernate.type.EntityType.resolve(EntityType.java:462)
    at org.hibernate.type.EntityType.resolve(EntityType.java:455)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)
    at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.resolveEntityKey(EntityReferenceInitializerImpl.java:142)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.resolveEntityKey(AbstractRowReader.java:143)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:94)
    at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:241)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4107)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:680)
    at org.hibernate.type.EntityType.resolve(EntityType.java:462)
    at org.hibernate.type.EntityType.resolve(EntityType.java:455)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)
    at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.resolveEntityKey(EntityReferenceInitializerImpl.java:142)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.resolveEntityKey(AbstractRowReader.java:143)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:94)
    at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:241)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4107)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:680)
    at org.hibernate.type.EntityType.resolve(EntityType.java:462)
    at org.hibernate.type.EntityType.resolve(EntityType.java:455)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)
    at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.resolveEntityKey(EntityReferenceInitializerImpl.java:142)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.resolveEntityKey(AbstractRowReader.java:143)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:94)
    at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:241)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4107)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:680)
    at org.hibernate.type.EntityType.resolve(EntityType.java:462)
    at org.hibernate.type.EntityType.resolve(EntityType.java:455)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)
    at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.resolveEntityKey(EntityReferenceInitializerImpl.java:142)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.resolveEntityKey(AbstractRowReader.java:143)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:94)
    at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:241)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4107)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:680)
    at org.hibernate.type.EntityType.resolve(EntityType.java:462)
    at org.hibernate.type.EntityType.resolve(EntityType.java:455)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)
    at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.resolveEntityKey(EntityReferenceInitializerImpl.java:142)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.resolveEntityKey(AbstractRowReader.java:143)
    at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:94)
    at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:241)
    at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4107)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1239)
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1122)
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:680)
    at org.hibernate.type.EntityType.resolve(EntityType.java:462)
    at org.hibernate.type.EntityType.resolve(EntityType.java:455)
    at org.hibernate.type.ComponentType.resolve(ComponentType.java:688)

the above Exception getting as infinite .

please look at my query

currentSession = sessionFactory.getCurrentSession();
            Query query = currentSession.createQuery("from AssocFeeStudent asf where  asf.studentDetails.studentAdmissionId= :studentAdmissionId");  
            query.setParameter("studentAdmissionId", id);  
            query.getResultList();     

please have a look on my entity classes

AssocFeeStudent

  @Entity
    @Table(name = "assoc_fee_student", catalog = "campus_guru_01")
    public class AssocFeeStudent implements java.io.Serializable {


        private FeeCategory feeCategory;
        private StudentDetails studentDetails;  

        public AssocFeeStudent() {
        }

        public AssocFeeStudent( FeeCategory feeCategory, StudentDetails studentDetails) {

            this.feeCategory = feeCategory;
            this.studentDetails = studentDetails;
        }

        @EmbeddedId

        @AttributeOverrides({
                @AttributeOverride(name = "feeCatId", column = @Column(name = "fee_cat_id", nullable = false)),
                @AttributeOverride(name = "studentId", column = @Column(name = "student_id", nullable = false)) })



        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "fee_cat_id", nullable = false)
        public FeeCategory getFeeCategory() {
            return this.feeCategory;
        }

        public void setFeeCategory(FeeCategory feeCategory) {
            this.feeCategory = feeCategory;
        }

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "student_id", nullable = false)
        public StudentDetails getStudentDetails() {
            return this.studentDetails;
        }

        public void setStudentDetails(StudentDetails studentDetails) {
            this.studentDetails = studentDetails;
        }

    }

and the dependent entities are

FeeCategory

@Entity
@Table(name = "fee_category", catalog = "campus_guru_01")

public class FeeCategory implements java.io.Serializable {

    private Integer feeCatId;
    private String feeCatName;
    private int parentId;
    private Set<AssocFeeTerms> assocFeeTermses = new HashSet<AssocFeeTerms>(0);
    private Set<FeeClassMapping> feeClassMappings = new HashSet<FeeClassMapping>(0);
    private Set<FeeTransactions> feeTransactionses = new HashSet<FeeTransactions>(0);
    private Set<AssocFeeStudent> assocFeeStudents = new HashSet<AssocFeeStudent>(0);

    public FeeCategory() {
    }

    public FeeCategory(int parentId) {
        this.parentId = parentId;
    }

    public FeeCategory(String feeCatName, int parentId, Set<AssocFeeTerms> assocFeeTermses,
            Set<FeeClassMapping> feeClassMappings, Set<FeeTransactions> feeTransactionses,
            Set<AssocFeeStudent> assocFeeStudents) {
        this.feeCatName = feeCatName;
        this.parentId = parentId;
        this.assocFeeTermses = assocFeeTermses;
        this.feeClassMappings = feeClassMappings;
        this.feeTransactionses = feeTransactionses;
        this.assocFeeStudents = assocFeeStudents;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "fee_cat_id", unique = true, nullable = false)
    public Integer getFeeCatId() {
        return this.feeCatId;
    }

    public void setFeeCatId(Integer feeCatId) {
        this.feeCatId = feeCatId;
    }

    @Column(name = "fee_cat_name")
    public String getFeeCatName() {
        return this.feeCatName;
    }

    public void setFeeCatName(String feeCatName) {
        this.feeCatName = feeCatName;
    }

    @Column(name = "parent_id", nullable = false)
    public int getParentId() {
        return this.parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }



@OneToMany(fetch = FetchType.EAGER, mappedBy = "feeCategory")
    public Set<AssocFeeStudent> getAssocFeeStudents() {
        return this.assocFeeStudents;
    }

    public void setAssocFeeStudents(Set<AssocFeeStudent> assocFeeStudents) {
        this.assocFeeStudents = assocFeeStudents;
    }

}

StudentDetails

@Entity
@Table(name = "student_details", catalog = "campus_guru_01")

public class StudentDetails implements java.io.Serializable {

    private Integer studentAdmissionId;
    private String isPrimaryyn;
    private String adsmissionNum;  
    private String firstname;
    private String lastname;
private Set<AssocFeeStudent> assocFeeStudents = new HashSet<AssocFeeStudent>(0);

@JsonIgnore  
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "studentDetails")
    public Set<AssocFeeStudent> getAssocFeeStudents() {
        return this.assocFeeStudents;
    }

    public void setAssocFeeStudents(Set<AssocFeeStudent> assocFeeStudents) {
        this.assocFeeStudents = assocFeeStudents;
    }

}

its strange to me not able to trace it . whats happening in this.

can any one solve it ?

thank you!.


回答1:


I hope you have proper reasons to have the kind of relationship mentioned in your query.

Anyway try fetching collection lazily i.e. instead of @OneToMany(fetch = FetchType.EAGER, mappedBy = "studentDetails") and @OneToMany(fetch = FetchType.EAGER, mappedBy = "feeCategory")

use @OneToMany(fetch = FetchType.LAZY, mappedBy = "studentDetails") and @OneToMany(fetch = FetchType.LAZY, mappedBy = "feeCategory")

And use @JsonBackReference to hint hibernate that back reference mapping is just a mirror image instead of new mapping

P.S. I strongly suggest you should revisit entity relationships



来源:https://stackoverflow.com/questions/50597110/infinite-loop-while-fetching-data-using-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!