2

There are 2 tables.

One table has many connection to the other table.

All connections are Lazy loading style. When I want to get something from UppeningUsers the lazy loading works and I can fetch the data. This part is clear.

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

This simply goes and gets proxy for listPhotoObj, peopleWhoBlockedMe, peopleIBlocked if I call them then they get initialized. So I understood this part.

However if I call

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

Then

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

This guy is proxy which means I cannot get any data. Even though I request get I do not get any information. Table 1 Table 1

Table 2 Table 2

So if I call from the mother table. We get the list when we want. This part is okay. enter image description here

Now the issue is if I call from the child table Where there is manytoOne relation.I get proxy from there . I do not get filled information. IF I do EAGER I will get it but I dont want that. Is there any other way I can get this lazy data when I need without doing EAGER. enter image description here

UPDATE 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;
}
   }

The UsersPhotos Class

    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;
}

}

UPDATE 2 I see that if I call any setter of User inside this UserPhoto object then it gets information. For example countryList .get(0).getUser().getLink() actually goes to database and retrieves information.. But only that information it does not respond as full object data which I mean countryList .get(0).getUser() this one. I dont want to go for all the data so thats my question.

UPDATE 3 Fetch sql suggestion was the answer however it created another question

   @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;

These 4 are creating SQL even though they are lazy loading.

Suppose that I just want the

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

then I do not actually needed those sqls happening.

legend12345
  • 162
  • 2
  • 14
  • Please, next time copy the source code instead of the print screen, so it can be more readable. In your last print screen, you have a lazy initialization of the user object which is okay as you tell hibernate to use `FetchType.LAZY`. Can you show how do you access `user` data? – O.Badr May 10 '17 at 23:13
  • List countryList = session.createQuery("from UsersPhotos").list(); I call this and that user data is null. I want to fill this user data but I want to keep lazy still. Is there something like forces when I want. To access I do countryList .get(0).getUser() and this simply does not return anything – legend12345 May 10 '17 at 23:26
  • I put the full source – legend12345 May 10 '17 at 23:52
  • Is this a question about hibernate or a question about how your debugger works? Hibernate appears to be doing exactly what you told it to. – Affe May 10 '17 at 23:59
  • Question is If I query from Photos I do not get UppeningUsers which is because of Lazy loading. But If I want that information to return from server then I get this lazy error. So I want to initialize the object so I can pass as server response. I dont want to do eager. If I use get of each variable then I get all the information. However I want the who Object of UppeningUser not just specific part of it. – legend12345 May 11 '17 at 00:02
  • `JOIN FETCH` load just the immediate association, – O.Badr May 11 '17 at 11:01

2 Answers2

0

You can use Hibernate.initialize() to trigger the fetching of any associated data, just make sure to use it inside the same session

Example:

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());
}
fujy
  • 5,168
  • 5
  • 31
  • 50
  • Sorry same thing happens. However through my investigation I see that if I call any setter of User inside this UserPhoto object then it gets information. For example countryList .get(0).getUser().getLink() actually goes to database and retrieves information.. But only that information it does not respond as full object data which I mean countryList .get(0).getUser() this one. I dont want to go for all the data so thats my question. – legend12345 May 10 '17 at 23:46
  • I put the full source – legend12345 May 10 '17 at 23:52
  • @legend12345 You are correct, one of the ways to trigger fetching the lazy initialized entities is to use call the getter method for them inside the session, Hibernate.initialize() is just a cleaner way provided by Hibernate just not to call a getter method in the code without assigning it to any variable – fujy May 10 '17 at 23:59
  • @legend12345 check this answer [here](http://stackoverflow.com/a/34809664/2727401) – fujy May 11 '17 at 00:00
  • @legend12345 you have to call it inside your transaction/session, i.e., before you close the session – fujy May 11 '17 at 00:06
  • @legend12345 Another way that you may consider that has been introduced in `JPA 2.1` is the `Named entity graphs` feature, you can find an example about it [here](http://www.thoughts-on-java.org/jpa-21-entity-graph-part-1-named-entity/) – fujy May 11 '17 at 00:08
  • Thanks for the answers I got it fixed. – legend12345 May 11 '17 at 00:12
  • 1
    @legend12345 This is a good listing of all the methods, in all cases you will have to implement something specifically to fetch these data http://www.thoughts-on-java.org/5-ways-to-initialize-lazy-relations-and-when-to-use-them/ – fujy May 11 '17 at 00:12
0

Try this :

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

Check this for more information: Difference between JOIN and JOIN FETCH in Hibernate

Update 1: You may use LEFT JOIN FETCH instead of JOIN FETCH so the query will not exclude UsersPhotos which doesn't have UppeningUsers.

List<UsersPhotos> countryList =  session.createQuery("from UsersPhotos up LEFT JOIN FETCH up.user").list();
O.Badr
  • 2,853
  • 2
  • 27
  • 36