0

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

Damien Gallagher
  • 981
  • 4
  • 13
  • 25

1 Answers1

1

@OneToOne declared this way is mapped by PK and it has to be eagerly fetched ...

you can :

  • make it a @ManyToOne(fetch=FetchType.LAZY)

OR

  • declare a foreign key column :

@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;
}
Pras
  • 1,068
  • 7
  • 18
  • Make it @ManyToOne on the ApplicationImageVO class is it? And a foreign key as mentioned in the ApplicationHomeScreenVO class? – Damien Gallagher Oct 05 '15 at 18:55
  • no no ... not everything ... you have the choice ... just change your mapping to "ManyToOne" ... it should work (hey, that's mapping into object ... ok it's one-to-one in DB, but in JPA it's like that ... ) ... OR, add a foreign key and don't link the id's ... – Pras Oct 05 '15 at 18:58
  • which approach would you recommend for an existing database structure? – Damien Gallagher Oct 05 '15 at 19:04
  • 1
    was simpler than i thought ... just changed the 4 `OtherEntity` annotations ... let me know if this works ... – Pras Oct 05 '15 at 19:10
  • haaaa thats amazing - I just tried that and it improved performance from 5 seconds for a call to 100 milliseconds. Hibernate is a unique tool :) – Damien Gallagher Oct 05 '15 at 19:20
  • 1
    Glad it helped ... accept the answer, it may help others ;) – Pras Oct 05 '15 at 19:22
  • it has indeed - thank you Pras - I think it has actually sorted a few performance pain points I was experiencing so thank you – Damien Gallagher Oct 05 '15 at 19:24