2016-08-17 133 views
2

中加载@OneToMany集合变得很慢我对应用程序进行了一些更改。我很确定,由于对实体的更改(在Netbeans IDE上进行了一些分析后),从数据库加载数据的速度变慢了。在eclipseLink和JPA

我添加的是一些方法和瞬态变量。 这是我的JPA实体的一个实体。确切的问题是它有什么问题吗?

/** 
* 
* @author Houssem 
*/ 
@Entity 
@Table(name = "t_feeder") 
@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Feeder.findAll", query = "SELECT f FROM Feeder f"), 
    @NamedQuery(name = "Feeder.findById", query = "SELECT f FROM Feeder f WHERE f.id = :id"), 
    @NamedQuery(name = "Feeder.findByIsVip", query = "SELECT f FROM Feeder f WHERE f.isVip = :isVip"), 
    @NamedQuery(name = "Feeder.findByFeederNumber", query = "SELECT f FROM Feeder f WHERE f.feederNumber = :feederNumber"), 
    @NamedQuery(name = "Feeder.findByPeriority", query = "SELECT f FROM Feeder f WHERE f.periority = :periority"), 
    @NamedQuery(name = "Feeder.findBySceneX", query = "SELECT f FROM Feeder f WHERE f.sceneX = :sceneX"), 
    @NamedQuery(name = "Feeder.findByPrimaryStation", query = "SELECT f FROM Feeder f WHERE f.primaryStationId = :ps"), 
    @NamedQuery(name = "Feeder.findByPsAndNumber", query = "SELECT f FROM Feeder f WHERE f.primaryStationId = :ps AND f.feederNumber = :number"), 
    @NamedQuery(name = "Feeder.findBySceneY", query = "SELECT f FROM Feeder f WHERE f.sceneY = :sceneY")}) 
public class Feeder implements Serializable { 

    private static final long serialVersionUID = 1L; 
    public static final String FIND_BY_NUMBER = "Feeder.findByFeederNumber"; 
    public static final String FIND_BY_PS = "Feeder.findByPrimaryStation"; 
    public static final String FIND_BY_PS_AND_NUMBER = "Feeder.findByPsAndNumber"; 

    transient private PropertyChangeSupport propertySupport; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Long id; 
    @Column(name = "is_vip") 
    private boolean isVip; 
    @Lob 
    @Column(name = "note", length = 65535) 
    private String note; 
    @Basic(optional = false) 
    @Column(name = "feederNumber") 
    private String feederNumber; 
    @Column(name = "periority") 
    private Integer periority; 
    @Column(name = "scene_x") 
    private Integer sceneX; 
    @Column(name = "scene_y") 
    private Integer sceneY; 
    @OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.LAZY, mappedBy = "feederId") 
    private Collection<SecondaryStation> secondaryStationCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "feederId") 
    private Collection<Port> portCollection; 
    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "feederId", fetch = FetchType.LAZY) 
    private Collection<StationContinuity> sourceStationContinuityCollection; 
    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "linkToFeederId", fetch = FetchType.LAZY) 
    private Collection<StationContinuity> targetStationContinuityCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "feederId", fetch = FetchType.LAZY) 
    private Collection<AlarmedFeeder> alarmedFeederCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "feederId", fetch = FetchType.LAZY) 
    private Collection<Comment> commentCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "feederId", fetch = FetchType.LAZY) 
    private Collection<FeederLoading> feederLoadingCollection; 
    @OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, mappedBy = "feederId", fetch = FetchType.LAZY) 
    private Collection<ConnectorNode> connectorNodeCollection; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "feederId") 
    private Collection<Signalization> signalizationCollection; 
    @JoinColumn(name = "primaryStationId", referencedColumnName = "id") 
    @ManyToOne(optional = false) 
    private PrimaryStation primaryStationId; 
    @OneToMany(mappedBy = "feederId") 
    private Collection<Link> linkCollection; 
    @Column(name = "date_synchronization") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date synchronizationDate; 
    @Column(name = "date_modification") 
    @Temporal(TemporalType.TIMESTAMP) 
    private Date modificationDate; 
    @Column(name = "synchronized") 
    private boolean isSynchronized; 

    //_______________ Export List Ref objects ________________ 
    transient private Collection<SecondaryStation> secondaryStationsRef; 
    transient private Collection<StationContinuity> sourceStationContinuitysRef; 
    transient private Collection<StationContinuity> targetStationContinuitysRef; 
    transient private Collection<Comment> commentsRef; 
    transient private Collection<ConnectorNode> connectorNodesRef; 
    transient private Collection<Signalization> signalizationsRef; 
    transient private Collection<EquipmentFileItem> equipmentFileItemsRef; 
    //Les port c'est automatique, puis avec get tjrs 

    //Getters & Setters - used only to add references, these objects are not added to DB 
    public Collection<EquipmentFileItem> getEquipmentFileItemsRef() { 
     return equipmentFileItemsRef; 
    } 

    public void setEquipmentFileItemsRef(Collection<EquipmentFileItem> equipmentFileItemsRef) { 
     this.equipmentFileItemsRef = equipmentFileItemsRef; 
    } 

    public Collection<SecondaryStation> getSecondaryStationsRef() { 
     return secondaryStationsRef; 
    } 

    public void setSecondaryStationsRef(Collection<SecondaryStation> secondaryStationCollection) { 
     this.secondaryStationsRef = secondaryStationCollection; 
    } 

    public Collection<StationContinuity> getSourceStationContinuitiesRef() { 
     return sourceStationContinuitysRef; 
    } 

    public void setSourceStationContinuitiesRef(Collection<StationContinuity> sourceStationContinuitysRef) { 
     this.sourceStationContinuitysRef = sourceStationContinuitysRef; 
    } 

    public Collection<StationContinuity> getTargetStationContinuitiesRef() { 
     return targetStationContinuitysRef; 
    } 

    public void setTargetStationContinuitiesRef(Collection<StationContinuity> targetStationContinuitysRef) { 
     this.targetStationContinuitysRef = targetStationContinuitysRef; 
    } 

    public Collection<Comment> getCommentsRef() { 
     return commentsRef; 
    } 

    public void setCommentsRef(Collection<Comment> commentsRef) { 
     this.commentsRef = commentsRef; 
    } 

    public Collection<ConnectorNode> getConnectorNodesRef() { 
     return connectorNodesRef; 
    } 

    public void setConnectorNodesRef(Collection<ConnectorNode> connectorNodesRef) { 
     this.connectorNodesRef = connectorNodesRef; 
    } 

    public Collection<Signalization> getSignalizationsRef() { 
     return signalizationsRef; 
    } 

    public void setSignalizationsRef(Collection<Signalization> signalizationsRef) { 
     this.signalizationsRef = signalizationsRef; 
    } 

    //_______________ End Export Stuff________________ 
    public Feeder() { 
     propertySupport = new PropertyChangeSupport(this); 
    } 

    public Feeder(Long id) { 
     this.id = id; 
    } 

    public Feeder(Long id, String feederNumber) { 
     this.id = id; 
     this.feederNumber = feederNumber; 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public boolean getIsVip() { 
     return isVip; 
    } 

    public void setIsVip(boolean isVip) { 
     this.isVip = isVip; 
    } 

    public String getNote() { 
     return note; 
    } 

    public void setNote(String note) { 
     this.note = note; 
    } 

    public String getFeederNumber() { 
     return feederNumber; 
    } 

    public void setFeederNumber(String feederNumber) { 
     this.feederNumber = feederNumber; 
    } 

    public Integer getPeriority() { 
     return periority; 
    } 

    public void setPeriority(Integer periority) { 
     this.periority = periority; 
    } 

    public Integer getSceneX() { 
     return sceneX; 
    } 

    public void setSceneX(Integer sceneX) { 
     this.sceneX = sceneX; 
    } 

    public Integer getSceneY() { 
     return sceneY; 
    } 

    public void setSceneY(Integer sceneY) { 
     this.sceneY = sceneY; 
    } 

    public Collection<SecondaryStation> fetchSecondaryStationCollection() { 
     return secondaryStationCollection; 
    } 

    public void setSecondaryStationCollection(Collection<SecondaryStation> secondaryStationCollection) { 
     this.secondaryStationCollection = secondaryStationCollection; 
    } 

    public Collection<Port> fetchPortCollection() { 
     return portCollection; 
    } 

    public Collection<Port> getPortCollection() { 
     return portCollection; 
    } 

    public void setPortCollection(Collection<Port> portCollection) { 
     this.portCollection = portCollection; 
    } 

    public Collection<StationContinuity> fetchSourceStationContinuityCollection() { 
     return sourceStationContinuityCollection; 
    } 

    public void setSourceStationContinuityCollection(Collection<StationContinuity> stationContinuityCollection) { 
     this.sourceStationContinuityCollection = stationContinuityCollection; 
    } 

    public Collection<StationContinuity> fetchTargetStationContinuityCollection() { 
     return targetStationContinuityCollection; 
    } 

    public void setTargetStationContinuityCollection(Collection<StationContinuity> stationContinuityCollection1) { 
     this.targetStationContinuityCollection = stationContinuityCollection1; 
    } 

    public Collection<AlarmedFeeder> fetchAlarmedFeederCollection() { 
     return alarmedFeederCollection; 
    } 

    public void setAlarmedFeederCollection(Collection<AlarmedFeeder> alarmedFeederCollection) { 
     this.alarmedFeederCollection = alarmedFeederCollection; 
    } 

    public Collection<Comment> fetchCommentCollection() { 
     return commentCollection; 
    } 

    public void setCommentCollection(Collection<Comment> commentCollection) { 
     this.commentCollection = commentCollection; 
    } 

    public Collection<FeederLoading> fetchFeederLoadingCollection() { 
     return feederLoadingCollection; 
    } 

    public void setFeederLoadingCollection(Collection<FeederLoading> feederLoadingCollection) { 
     this.feederLoadingCollection = feederLoadingCollection; 
    } 

    public Collection<ConnectorNode> fetchConnectorNodeCollection() { 
     return connectorNodeCollection; 
    } 

    public void setConnectorNodeCollection(Collection<ConnectorNode> connectorNodeCollection) { 
     this.connectorNodeCollection = connectorNodeCollection; 
    } 

    public Collection<Signalization> fetchSignalizationCollection() { 
     return signalizationCollection; 
    } 

    public void setSignalizationCollection(Collection<Signalization> signalizationCollection) { 
     this.signalizationCollection = signalizationCollection; 
    } 

    public long getPrimaryStationId() { 
     if (primaryStationId != null) { 
      return primaryStationId.getId(); 
     } 
     return 0; 
    } 

    public PrimaryStation fetchPrimaryStation() { 
     return primaryStationId; 
    } 

    public void setPrimaryStationId(PrimaryStation primaryStationId) { 
     this.primaryStationId = primaryStationId; 
    } 

    public Collection<Link> fetchLinkCollection() { 
     return linkCollection; 
    } 

    public void setLinkCollection(Collection<Link> linkCollection) { 
     this.linkCollection = linkCollection; 
    } 

    public void addPropertyChangeListener(PropertyChangeListener listener) { 
     propertySupport.addPropertyChangeListener(listener); 
    } 

    public void removePropertyChangeListener(PropertyChangeListener listener) { 
     propertySupport.removePropertyChangeListener(listener); 
    } 

    public Date getSynchronizationDate() { 
     return synchronizationDate; 
    } 

    public void setSynchronizationDate(Date synchronizationDate) { 
     this.synchronizationDate = synchronizationDate; 
    } 

    public Date getModificationDate() { 
     return modificationDate; 
    } 

    public void setModificationDate(Date modificationDate) { 
     this.modificationDate = modificationDate; 
    } 

    public boolean isIsSynchronized() { 
     return isSynchronized; 
    } 

    public void setIsSynchronized(boolean isSynchronized) { 
     this.isSynchronized = isSynchronized; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof Feeder)) { 
      return false; 
     } 
     Feeder other = (Feeder) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "Feeder[ number=" + this.feederNumber + " ]"; 
    } 

} 
+2

打开SQL日志记录并查看正在生成的查询。没有这些,你只是在猜测。 https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging –

+1

另外,一个分析器可以帮助告诉你'多余'时间在哪里。这个实体没有太明显的错误 - 魔鬼在详细说明你如何使用它。由于OneToMany关系在默认情况下是懒惰的,您如何访问它们,您发现它们很慢,您如何获取最初的'feeder'实例以及数据库中有多少条记录? – Chris

回答

1

实体馈线有很多!!!!一对多的关系,因为你所有的查询都会很慢(eclipselink没有问题)。如果你将所有的一对多转换为单向的多对一会更有效率(也是实体看起来更清晰和易于理解)。

+0

我该如何做转换?你的意思是一个中间表? –