gpt4 book ai didi

hibernate - JPA 2.1 通过提示查找,如果@NamedEntityGraph 中存在多个@NamedSubgraph,则返回冗余结果

转载 作者:行者123 更新时间:2023-12-02 04:45:49 24 4
gpt4 key购买 nike

我在我的项目中结合使用 **JPA 2.1** 和 **Hibernate 5.0.2.final**。

问题是,当我加载类型为 {@EventTO} 的实例时,如果该实例一次没有与会者和 assignedResources 或这些集合之一,它将正确加载;但是,如果它同时拥有这两个集合,则实体图的提取结果将携带冗余数据。例如,如果有 3 个与会者和 2 个 assignedResources,作为线索,它将返回 4 个与会者和 3 个 assignedResources。我对这样的行为感到困惑。我不知道为什么???。可能与 hibernate 查询有关,但我不确定。

现在,如果有人能帮我找到解决这个问题的天才解决方案,我将不胜感激。

提到的实体以及调用 JPA 实体管理器的 find 方法的代码片段如下:

EventTO 实体:

@Entity
@Table(name = "CALT_EVENT")
@SequenceGenerator(name = "seq", sequenceName = "seq_cal_event", allocationSize = 1)
@NamedEntityGraph(name = "loadGraph",
attributeNodes = {
@NamedAttributeNode(value = "attendees", subgraph = "attendees"),
@NamedAttributeNode(value = "assignedResources", subgraph = "assignedResources")
},

subgraphs = {
@NamedSubgraph(name = "attendees", attributeNodes = {@NamedAttributeNode("departmentAttendee"), @NamedAttributeNode(value = "personAttendee")}),
@NamedSubgraph(name = "assignedResources", attributeNodes = @NamedAttributeNode("resourceTO"))
}
)
})
public class EventTO {
private List<EventAttendeeTO> attendees = new ArrayList<EventAttendeeTO>();
private List<EventAssignedResourceTO> assignedResources = new ArrayList<EventAssignedResourceTO>();

/**
* @return the attendeesTOList
*/
@OneToMany(mappedBy = "eventTO", cascade = CascadeType.ALL, orphanRemoval = true)
public List<EventAttendeeTO> getAttendees() {
return attendees;
}

/**
* @param attendees the attendeesTOList to set
*/
public void setAttendees(List<EventAttendeeTO> attendees) {
if (attendees != null) {
this.attendees = attendees;
}
}

/**
* @return the assignedResourceTOList
*/
@OneToMany(cascade = CascadeType.ALL, mappedBy = "eventTO", orphanRemoval = true)
public List<EventAssignedResourceTO> getAssignedResources() {
return assignedResources;
}

/**
* @param assignedResources the assignedResourceTOList to set
*/
public void setAssignedResources(List<EventAssignedResourceTO> assignedResources) {
if (assignedResources != null) {
this.assignedResources = assignedResources;
}
}
}


EventAttendeeTO 实体:

@Entity
@Table(name = "CALT_EVENT_ATTENDEES")
@SequenceGenerator(name = "seq", sequenceName = "seq_cal_event_attendees", allocationSize = 1)
public class EventAttendeeTO extends RecordableExtEntityTO {
private EventTO eventTO;
private PersonTO personAttendee;
private DepartmentTO departmentAttendee;

/**
* @return the eventTO
*/
@JSON(serialize = false)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAT_EVN_ID", nullable = false)
public EventTO getEventTO() {
return eventTO;
}

/**
* @param eventTO
* the eventTO to set
*/
public void setEventTO(EventTO eventTO) {
this.eventTO = eventTO;
}

/**
* @return the attendee
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAT_PER_ID")
public PersonTO getPersonAttendee() {
return personAttendee;
}

/**
* @param personAttendee
* the personAttendee to set
*/
public void setPersonAttendee(PersonTO personAttendee) {
this.personAttendee = personAttendee;
if (personAttendee != null) {
this.perId = personAttendee.getId();
}
}

}


EventAssignedResourceTO 实体:

@Entity
@Table(name = "CALT_EVENT_ASSIGNED_RESOURCE")
@SequenceGenerator(name = "seq", sequenceName = "seq_cal_event_assigned_res", allocationSize = 1)
public class EventAssignedResourceTO extends ExtEntityTO {

private EventTO eventTO;
private EventResourceTO resourceTO;

/**
* @return the eventTO
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAR_EVN_ID", nullable = false)
public EventTO getEventTO() {
return eventTO;
}

/**
* @param eventTO
* the eventTO to set
*/
public void setEventTO(EventTO eventTO) {
this.eventTO = eventTO;
}

/**
* @return the resourceTO
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EAR_ERS_ID")
public EventResourceTO getResourceTO() {
return resourceTO;
}

/**
* @param resourceTO
* the resourceTO to set
*/
public void setResourceTO(EventResourceTO resourceTO) {
this.resourceTO = resourceTO;
}
}


通过hint方式调用entity manager的find方法的代码片段:

    try {
EntityGraph graph = this.em.getEntityGraph("loadGraph");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);
return em.find(EventTO.class, id, hints);
} catch (Exception e) {
new DAOException(e);
}

最佳答案

在修改我的代码并仔细思考这个问题之后,我想到了以下原因:
此问题可能与 hibernate 在同时加载多个集合方面的限制有关。由于我的 ORM 处于 hibernate 状态,因此在获取我的 TO 时可能会出现这样的问题。

更多详细信息:Hibernate 在一次查询中获取多个包时抛出以下异常:

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags


也许使用 NamedGraph 会导致 hibernate 创建基于错误的外部左连接的查询,这会导致获取冗余或换句话说无效数据。

关于hibernate - JPA 2.1 通过提示查找,如果@NamedEntityGraph 中存在多个@NamedSubgraph,则返回冗余结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33263154/

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