2016-02-14 1 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; 
    } 
} 

, если я хочу получить locationEntity с locationid 10, то Collection<NodesEntity> nodesLocation будет загружен, а также все коллекции в NodesEntity будет загружен и так далее, а режим fetch является Lazy :(. так что эта проблема ухудшать производительность и вызвало другие проблемы ....
в DAO слое я использую EntityManager как этот

@PersistenceContext 
    private EntityManager entityManager; 

и использовать entityManager.find() для извлечения записей.

Почему это происходит? и как я могу это исправить?

ответ

0

попытаться использовать entityManager.getReference(<Class-name>, <id>); состояние это ленивый выбор.

см ссылку: http://www.objectdb.com/java/jpa/persistence/retrieve

+0

я попробовать 'entityManager.getReference (, )', но он не работает. в любом случае, спасибо. –

Смежные вопросы