Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate JPA 2.1按提示查找,如果@NamedEntityGraph中存在多个@NamedSubgraph,则返回冗余结果_Hibernate_Jpa_Repeat_Subgraph - Fatal编程技术网

Hibernate JPA 2.1按提示查找,如果@NamedEntityGraph中存在多个@NamedSubgraph,则返回冗余结果

Hibernate JPA 2.1按提示查找,如果@NamedEntityGraph中存在多个@NamedSubgraph,则返回冗余结果,hibernate,jpa,repeat,subgraph,Hibernate,Jpa,Repeat,Subgraph,在我的项目中,我将**JPA 2.1**与**Hibernate 5.0.2.final**结合使用。 问题是,当我加载类型为{@EventTO}的实例时,如果该实例一次没有与会者和assignedResources或其中一个集合,它将正确加载;但是,如果同时具有这两个集合,则实体图获取的结果将携带冗余数据。例如,如果有3个与会者和2个assignedResources,作为提示,它将返回4个与会者和3个assignedResources。我对这种行为感到困惑。我不知道为什么???。可能与hi

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

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

现在,如果有人能帮我找到这个问题的天才解决方案,我将不胜感激。 上述实体以及调用JPA entity manager的find方法的代码段如下:

事件到实体:


事件分配给实体的资源:


通过提示调用实体管理器的find方法的代码段:


在修改了我的代码并对这个问题进行了长时间的思考之后,我想到了以下原因:
此问题可能与hibernate限制同时加载多个集合有关。由于我的ORM是hibernate,它可能会在将我的ORM获取到时造成这样的问题。

更多详细信息:Hibernate在一次查询获取多个行李时引发以下异常:

org.hibernate.loader.MultipleBagFetchException:无法同时执行 取多个包



使用NamedGraph可能会导致hibernate基于错误的外部左连接创建查询,这会导致获取冗余数据或无效数据

我的理解是hibernate创建查询,然后从数据库中获取行列表。每一行都包含所有数据:事件+与会者+资源,因此无法跟踪列表中的实体数量。 我不知道这在所有情况下都是可能的,但我用集合来解决这个问题。 请记住,在使用集合时,需要实现equals(和hashCode)方法

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

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