2016-02-14 21 views
0

我使用的是spring boot v.1.3和hibernate v.4.3.11.Final。Entitymanager忽略延遲獲取模式的休眠和加載每個相關記錄

當我想從數據庫中獲取單個記錄時,所有相關記錄都將被加載。例如,對於這個實體

@Entity 
@Table(name = "LOCATIONS", schema = "xxxxx", catalog = "") 
public class LocationsEntity { 
    private long locationid; 
    private String address; 
    private Double lan; 
    private Double lat; 
    private String name; 
    private Collection<NodesEntity> nodesLocation; 

    @Id 
    @GeneratedValue 
    @Column(name = "LOCATIONID", nullable = false, insertable = true, updatable = true, precision = 0) 
    public long getLocationid() { 
     return locationid; 
    } 

    public void setLocationid(long locationid) { 
     this.locationid = locationid; 
    } 

    @Basic 
    @Column(name = "ADDRESS", nullable = true, insertable = true, updatable = true, length = 255) 
    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    @Basic 
    @Column(name = "LAN", nullable = true, insertable = true, updatable = true) 
    public Double getLan() { 
     return lan; 
    } 

    public void setLan(Double lan) { 
     this.lan = lan; 
    } 

    @Basic 
    @Column(name = "LAT", nullable = true, insertable = true, updatable = true) 
    public Double getLat() { 
     return lat; 
    } 

    public void setLat(Double lat) { 
     this.lat = lat; 
    } 

    @Basic 
    @Column(name = "NAME", nullable = true, insertable = true, updatable = true, length = 255) 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     LocationsEntity that = (LocationsEntity) o; 

     if (locationid != that.locationid) return false; 
     if (address != null ? !address.equals(that.address) : that.address != null) return false; 
     if (lan != null ? !lan.equals(that.lan) : that.lan != null) return false; 
     if (lat != null ? !lat.equals(that.lat) : that.lat != null) return false; 
     if (name != null ? !name.equals(that.name) : that.name != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = (int) (locationid^(locationid >>> 32)); 
     result = 31 * result + (address != null ? address.hashCode() : 0); 
     result = 31 * result + (lan != null ? lan.hashCode() : 0); 
     result = 31 * result + (lat != null ? lat.hashCode() : 0); 
     result = 31 * result + (name != null ? name.hashCode() : 0); 
     return result; 
    } 

    @OneToMany(mappedBy = "location", fetch = FetchType.LAZY) 
    public Collection<NodesEntity> getNodesLocation() { 
     return nodesLocation; 
    } 

    public void setNodesLocation(Collection<NodesEntity> nodesByLocationid) { 
     this.nodesLocation = nodesByLocationid; 
    } 
} 

,如果我想獲得locationEntitylocationid 10則Collection<NodesEntity> nodesLocation將被載入並且還NodesEntity所有收集將被加載等等,而fetch模式Lazy :(。所以這個問題降低性能並造成其他問題....
在DAO層我用EntityManager這樣

@PersistenceContext 
    private EntityManager entityManager; 

,並使用entityManager.find()檢索記錄。

爲什麼會發生這種情況?我該如何解決它?

回答