gpt4 book ai didi

java - 使用 hibernate 获取数据时无限循环

转载 作者:行者123 更新时间:2023-11-30 06:09:23 26 4
gpt4 key购买 nike

我正在使用HQl获取数据,但查询时发生一个奇怪的异常,请看一下

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)

上述异常变得无限。

请查看我的查询

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

请查看我的实体类

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

}

依赖实体是

费用类别

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

}

学生详细信息

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

}

我无法追踪它,这对我来说很奇怪。这是怎么回事。

有人能解决吗?

谢谢!

最佳答案

我希望您有适当的理由建立查询中提到的那种关系。

无论如何,请尝试延迟获取集合,即代替@OneToMany(fetch = FetchType.EAGER,mappedBy = "studentDetails")@OneToMany(fetch = FetchType.EAGER,mappedBy = "feeCategory")

使用@OneToMany(fetch = FetchType.LAZY,mappedBy = "studentDetails")@OneToMany(fetch = FetchType.LAZY,mappedBy = "feeCategory")

并使用@JsonBackReference来提示hibernate反向引用映射只是一个镜像而不是新的映射

附注我强烈建议您重新审视实体关系

关于java - 使用 hibernate 获取数据时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50597110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com