JPA hibernate 4延迟加载问题。如何在ManyToOne中加载延迟数据而不需要

时间:2017-05-10 21:01:19

标签: java hibernate jpa hibernate-4.x

有两张桌子。

一个表与另一个表有很多连接。

所有连接都是延迟加载样式。当我想从UppeningUsers获取一些东西时,延迟加载工作,我可以获取数据。这部分很清楚。

Session session = this.sessionFactory.getCurrentSession();
    List<UppeningUsers> countryList =  session.createQuery("from UppeningUsers").list();

这简直就是获取listPhotoObj,peopleWhoBlockedMe,peopleIBlocked的代理,如果我调用它们然后它们被初始化。所以我理解了这一部分。

但是如果我打电话

      Session session = this.sessionFactory.getCurrentSession();
List<UsersPhotos> countryList =  session.createQuery("from UsersPhotos").list();

然后

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UppeningUsers user;

这家伙是代理人,这意味着我无法获得任何数据。 即使我要求得到我也没有得到任何信息。 表格1 Table 1

表2 Table 2

所以,如果我从母亲的桌子打电话。我们在需要时获取列表。这部分没关系。 enter image description here

现在的问题是,如果我从子表调用哪里有很多关系。我从那里得到代理。我没有填写信息。 如果我做EAGER我会得到它但我不想要那个。有没有其他方法可以在不需要EAGER的情况下获取这些懒惰的数据。 enter image description here

更新1

    package com.uppening.models;

    import com.sun.istack.internal.Nullable;
    import org.hibernate.annotations.Formula;
    import org.hibernate.annotations.Proxy;

    import javax.persistence.*;
    import java.util.Set;


   @Entity
  @Table(name = "uppening_users")
  @Proxy(lazy = true)

   public class UppeningUsers {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;

private String name;
private boolean isblocked;

private String mail;
private String birthday;
private String source;
private String gender;
private String link;

private String description;
private String traveller;
private String interests;
private String device;
private String location;
private String showup;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UsersPhotos> listPhotoObj;

@OneToMany(mappedBy = "personBlocked", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserBlocks> peopleWhoBlockedMe;

@OneToMany(mappedBy = "blocker", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserBlocks> peopleIBlocked;

@OneToMany(mappedBy = "activityUser", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserActivities> listActivities;


@Formula(" DATE_FORMAT( FROM_DAYS( TO_DAYS( NOW( ) ) - TO_DAYS( birthday ) ) ,  '%Y' )    ")
@Nullable
private Integer age;














public UppeningUsers() {
    super();
}

@Transient
public Integer getAge() {
    return age;
}

public UppeningUsers(String name, boolean isblocked, String mail, String birthday,
                     String source, String gender, String link, String description,
                     String traveller, String interests, String device, String location,
                     String showup, Set<UsersPhotos> listPhotoObj, Set<UserBlocks> peopleWhoBlockedMe,
                     Set<UserBlocks> peopleIBlocked, Set<UserActivities> listActivities, Integer age) {
    this.name = name;
    this.isblocked = isblocked;
    this.mail = mail;
    this.birthday = birthday;
    this.source = source;
    this.gender = gender;
    this.link = link;
    this.description = description;
    this.traveller = traveller;
    this.interests = interests;
    this.device = device;
    this.location = location;
    this.showup = showup;
    this.listPhotoObj = listPhotoObj;
    this.peopleWhoBlockedMe = peopleWhoBlockedMe;
    this.peopleIBlocked = peopleIBlocked;
    this.listActivities = listActivities;
    this.age = age;
}
public String getName() {
    return name;
}

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

public boolean isIsblocked() {
    return isblocked;
}

public void setIsblocked(boolean isblocked) {
    this.isblocked = isblocked;
}

public String getMail() {
    return mail;
}

public void setMail(String mail) {
    this.mail = mail;
}

public String getBirthday() {
    return birthday;
}

public void setBirthday(String birthday) {
    this.birthday = birthday;
}

public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getTraveller() {
    return traveller;
}

public void setTraveller(String traveller) {
    this.traveller = traveller;
}

public String getInterests() {
    return interests;
}

public void setInterests(String interests) {
    this.interests = interests;
}

public String getDevice() {
    return device;
}

public void setDevice(String device) {
    this.device = device;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getShowup() {
    return showup;
}

public void setShowup(String showup) {
    this.showup = showup;
}

public Set<UsersPhotos> getListPhotoObj() {
    return listPhotoObj;
}

public void setListPhotoObj(Set<UsersPhotos> listPhotoObj) {
    this.listPhotoObj = listPhotoObj;
}

public Set<UserBlocks> getPeopleWhoBlockedMe() {
    return peopleWhoBlockedMe;
}

public void setPeopleWhoBlockedMe(Set<UserBlocks> peopleWhoBlockedMe) {
    this.peopleWhoBlockedMe = peopleWhoBlockedMe;
}

public Set<UserBlocks> getPeopleIBlocked() {
    return peopleIBlocked;
}

public void setPeopleIBlocked(Set<UserBlocks> peopleIBlocked) {
    this.peopleIBlocked = peopleIBlocked;
}

public Set<UserActivities> getListActivities() {
    return listActivities;
}

public void setListActivities(Set<UserActivities> listActivities) {
    this.listActivities = listActivities;
}

public void setAge(Integer age) {
    this.age = age;
}
   }

UsersPhotos类

    package com.uppening.models;

    import org.hibernate.annotations.Proxy;

    import javax.persistence.*;

    @Entity
    @Table(name="uppening_resimler")
    @Proxy(lazy = true)

    public class UsersPhotos {

@Id
@Column(name="id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
int id;

@Column(name="photo")
private
String photo;
public UsersPhotos() {
    super();
}

public UsersPhotos(String photo, UppeningUsers user) {
    this.photo = photo;
    this.user = user;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private UppeningUsers user;

public String getPhoto() {
    return photo;
}

public void setPhoto(String photo) {
    this.photo = photo;
}

public UppeningUsers getUser() {
    return user;
}

public void setUser(UppeningUsers user) {
    this.user = user;
}

}

更新2 我看到如果我在这个UserPhoto对象中调用任何用户的setter,那么它将获取信息。例如countryList .get(0).getUser()。getLink()实际上转到数据库并检索信息..但只有那些信息它不作为完整的对象数据响应,我的意思是countryList .get(0).getUser()这个。我不想要获取所有数据,这就是我的问题。

更新3 获取sql建议是答案,但它创建了另一个问题

   @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UsersPhotos> listPhotoObj;

@OneToMany(mappedBy = "personBlocked", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserBlocks> peopleWhoBlockedMe;

@OneToMany(mappedBy = "blocker", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserBlocks> peopleIBlocked;

@OneToMany(mappedBy = "activityUser", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserActivities> listActivities;

即使它们是延迟加载,这4个也在创建SQL。

假设我只想要

private String mail;
private String birthday;
private String source;
private String gender;
private String link;

然后我实际上并不需要那些风暴。

2 个答案:

答案 0 :(得分:0)

您可以使用Hibernate.initialize()触发获取任何关联数据,只需确保在同一session

内使用它

示例:

Session session = this.sessionFactory.getCurrentSession();
List<UsersPhotos> countryList =  session.createQuery("from UsersPhotos").list();

for(UsersPhotos usersPhotos : countryList){ // don't forget the null countryList case
  Hibernate.initialize(usersPhotos.getUser());
}

答案 1 :(得分:0)

试试这个:

List<UsersPhotos> countryList =  session.createQuery("from UsersPhotos up JOIN FETCH up.user").list();

检查以获取更多信息:multivalued

更新1: 您可以使用Difference between JOIN and JOIN FETCH in Hibernate代替LEFT JOIN FETCH,因此查询不会排除UsersPhotos没有UppeningUsers

List<UsersPhotos> countryList =  session.createQuery("from UsersPhotos up LEFT JOIN FETCH up.user").list();