I ran into a problem that individual lazy loading does not work in sleep mode. I already decided it , but still itβs not right to understand what is happening.
My code ( lazy loading doesn't work here when I pull out Person - Address is also selected):
@Entity public class Person{ @Id @SequenceGenerator(name = "person_sequence", sequenceName = "sq_person") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_sequence") @Column(name = "id") private long personID; @OneToOne(mappedBy="person", cascade=CascadeType.ALL, fetch = FetchType.LAZY) private Adress address; //.. getters, setters } @Entity public class Address { @Id @Column(name="id", unique=true, nullable=false) @GeneratedValue(generator="gen") @GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="person")) private long personID; @PrimaryKeyJoinColumn @OneToOne private FileInfo person; }
But : if I add optional=false to the OneToOne relationship, lazy loading works fine !
@OneToOne(mappedBy="person", cascade=CascadeType.ALL, optional = false, fetch = FetchType.LAZY) private Adress address;
Question / Entreaty: please explain to me how optional=false annotation helps to achieve lazy loading.
PS I read the posts post1 and post2 , and understand why a simple OneToOne cannot be lazy, but I still can not understand the magic of optional=false .
hibernate jpa lazy-loading one-to-one
Volodymyr Bakhmatiuk Aug 01 '13 at 7:24 on 2013-08-01 07:24
source share