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
So if I call from the mother table. We get the list when we want. This part is okay.
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.
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.