2

I have following entities:

Employee [@Id Long id, String name, @OneToOne Address branchAddress, @OneToOne Address homeAddress]

Address [@Id Long id, String street, String City, String zipcode, Country country]

Country [@Id Long id, String name, String currency, String isoCode]

My problem is when I try to load Employee, most of the time I don't want address. Fetching address fetches address and subsequenty country. These are extra queries that are executed everytime I load employee.

I have tried making @OneToOne(FetchType.LAZY) but doesn't work, it still pulls unneeded information.

How can i avoid fetching composite objects?

I would like the end result to not contain Address objects, Since it is needed once in 20 scenarios I can fetch them separately.

Any ideas, how to achieve this.

Thanks

1 Answers1

1

If our oneToOne value is not nullable, you can achieve lazy loading by adding optional=false : @OneToOne(optional=false, fetch = FetchType.LAZY).

It is perfectly explained here .

buræquete
  • 12,943
  • 4
  • 34
  • 69
baraber
  • 3,086
  • 25
  • 41