The mappedBy was missing from the "working" test ... but that is required to ensure that an extra foreign key is not generated in the Person table. After adding the mappedBy to the "working" test ... it also fails to work correctly - which is to say, fetching the pet4 and pet5 entities is not done lazily. The fetch is performed aggressively (two extra queries) when executing the Person query. From the table mappings, I'm not understanding why the extra query would be required.
The model can be simplified as below and the underlying table for Person will have nothing more than its key, and the table for Pet4 will have its key and owner_id as a foreign key to person. Using the same basic test code, it can be seen that the query for person also results in a second query for Pet4.
@Entity public class Person { @Id private long id;
@OneToOne( cascade= {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE}
, fetch=FetchType.LAZY, mappedBy="owner") @LazyToOne(value = LazyToOneOption.NO_PROXY) private Pet4 pet4;
// setters/getters here }
@Entity Pet4 { @Id private long id;
@OneToOne(fetch=FetchType.LAZY, cascade= {CascadeType.MERGE, CascadeType.PERSIST}
) @LazyToOne(value = LazyToOneOption.NO_PROXY) @JoinColumn(name="OWNER_ID") private Person owner;
// setters/getters here }
|