Entitymanager忽略hibernate的延迟提取模式并加载每个相关记录

时间:2016-02-14 16:40:53

标签: hibernate spring-boot hibernate-entitymanager

我正在使用spring boot v.1.3和hibernate v.4.3.11.Final。

当我想从数据库中获取单个记录时,将加载所有相关记录。例如,对于这个实体

@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中的所有集合都将被加载,等等{{1}模式是fetch :(。所以这个问题会降低性能并导致其他问题.... 在DAO层我使用Lazy像这样

EntityManager

并使用@PersistenceContext private EntityManager entityManager; 检索记录。

为什么会这样?我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试使用entityManager.getReference(<Class-name>, <id>); 它的状态是懒惰的。

请参阅链接:http://www.objectdb.com/java/jpa/persistence/retrieve