I have 3 object that interfacing each other like that:
@Entity
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@OneToMany(mappedBy = "room", fetch = FetchType.EAGER)
private List<SponsorStart> sponsorsStart = new ArrayList<>();
@OneToMany(mappedBy = "room", fetch = FetchType.LAZY)
private List<SponsorFinish> sponsorsFinish = new ArrayList<>();
//getters and setters
```
@Entity
public class SponsorStart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "room_id")
private Room room;
@OneToMany(cascade = CascadeType.ALL)
private List<Post> posts;
//getters and setters
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private long pk;
@Column
private String postId;
//getters and setters
if set settings like top then results here: Note that displayed 4 rows (3 if start from 0) But if i want to get list of post form SponsorStart object i get:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring.givestracker.model.SponsorStart.posts, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:147)
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:303)
at com.spring.givestracker.service.impl.SponsorsFinishServiceImpl.parseSponsorFinishByRoom(SponsorsFinishServiceImpl.java:78)
Here is lazy:
for (SponsorStart sponsorStart : room.getSponsorsStart()) {
Hibernate.initialize(sponsorStart.getPosts());
}
Okay, i thought i know problem and in SponsorStart Entity added:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Post> posts;
But! I have this problem! Yes here is problem! I have total 43 post in 4 SonsorStart entity, why it displayed like that and what should i do to fix it?
Help)
UPDATE 1
@Entity
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@OneToMany(mappedBy = "room", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<SponsorStart> sponsorsStart = new ArrayList<>();
@OneToMany(mappedBy = "room", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<SponsorFinish> sponsorsFinish = new ArrayList<>();
//getters and setters
@Entity
public class SponsorStart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne (fetch=FetchType.LAZY,
cascade=CascadeType.ALL)
@JoinColumn(name = "room_id")
private Room room;
@OneToMany(mappedBy = "sponsorStart", cascade = CascadeType.ALL)
private List<Post> posts;
//getters and setters
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private long pk;
@Column
private String postId;
@ManyToOne(fetch=FetchType.LAZY)
private SponsorStart sponsorStart;
//getters and setters
And i faced with lazy initialization
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring.givestracker.model.SponsorStart.posts, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:597)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:216)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576)
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:744)
at org.hibernate.Hibernate.initialize(Hibernate.java:64)
at com.spring.givestracker.service.impl.SponsorsFinishServiceImpl.parseSponsorFinishByRoom(SponsorsFinishServiceImpl.java:73)
From RoomController using RoomService i get findRoomById(id), RoomService has @Transactional, and from RoomController i gave the Room was found to another service. When i delegated another service used RoomService to findRoomById(id) the problem with Lazy the problem disappeared! But i dont know what was that when i have 4 sponsors but hibernate load me 43 times 4 sponsors) because sponsors get 43 posts...