Reputation: 991
If I have the following classes in my project
@Entity
@Table(name = "application_home_screen")
public class ApplicationHomeScreenVO implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@OneToOne(fetch = FetchType.EAGER)
@Cascade({ CascadeType.ALL })
@JoinColumn(name="image1_id")
private ApplicationImageVO image1;
@OneToOne(fetch = FetchType.EAGER)
@Cascade({ CascadeType.ALL })
@JoinColumn(name="image2_id")
private ApplicationImageVO image2;
}
@Entity
@Table(name = "application_image")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ApplicationImageVO implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image1")
@Cascade({ CascadeType.ALL })
private ApplicationHomeScreenVO homeScreenImage1;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image2")
@Cascade({ CascadeType.ALL })
private ApplicationHomeScreenVO homeScreenImage2;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity1")
@Cascade({ CascadeType.ALL })
private OtherEntity1 otherEntity1;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity2")
@Cascade({ CascadeType.ALL })
private OtherEntity2 otherEntity2;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity3")
@Cascade({ CascadeType.ALL })
private OtherEntity3 otherEntity3;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity4")
@Cascade({ CascadeType.ALL })
private OtherEntity4 otherEntity3;
}
When I load the ApplicationHomeVO - id expect only image1 and image2 to be loaded from the ApplicationImageVO class but that is not the case
All the other objects in the ApplicationImageVO class are also loaded even though they are marked as LAZY. Id expect only the ApplicationHomeScreenVO objects to be loaded
Is there any way to stop these other entites from being loaded?
Thank you Damie
Upvotes: 0
Views: 189
Reputation: 1098
@OneToOne declared this way is mapped by PK and it has to be eagerly fetched ...
you can :
@ManyToOne(fetch=FetchType.LAZY)
OR
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="fk")
public T getT()
EDIT
@Entity
@Table(name = "application_home_screen")
public class ApplicationHomeScreenVO implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@OneToOne(fetch = FetchType.EAGER)
@Cascade({ CascadeType.ALL })
@JoinColumn(name="image1_id")
private ApplicationImageVO image1;
@OneToOne(fetch = FetchType.EAGER)
@Cascade({ CascadeType.ALL })
@JoinColumn(name="image2_id")
private ApplicationImageVO image2;
}
@Entity
@Table(name = "application_image")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ApplicationImageVO implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image1")
@Cascade({ CascadeType.ALL })
private ApplicationHomeScreenVO homeScreenImage1;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "image2")
@Cascade({ CascadeType.ALL })
private ApplicationHomeScreenVO homeScreenImage2;
@ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity1")
@Cascade({ CascadeType.ALL })
private OtherEntity1 otherEntity1;
@ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity2")
@Cascade({ CascadeType.ALL })
private OtherEntity2 otherEntity2;
@ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity3")
@Cascade({ CascadeType.ALL })
private OtherEntity3 otherEntity3;
@ManyToOne(fetch = FetchType.LAZY, mappedBy = "otherEntity4")
@Cascade({ CascadeType.ALL })
private OtherEntity4 otherEntity3;
}
Upvotes: 1