I\'m developing a relatively large project using Spring Boot, and in a general way I\'m pretty happy with it, but I\'m having some problems that in my mind should\'t be a pr
If the join column is not in the table to which a parent in a one-to-one association is mapped, then the association cannot be lazy. The reason is that JPA provider cannot determine whether to create the proxy, so that it can load the object when accessed later, or leave the null value.
Even if the association is not optional, JPA provider has to determine the id of the associated entity instance to store it in the proxy. Thus it has to go to the associated table anyway.
Solutions:
null, otherwise use list.get(0). You can of course encapsulate this in the entity class (getter returns the only element of the list or null). The drawback is that you will have to treat this as collection in JPQL queries.optional = false), then JPA provider knows that there is an associated child with the same PK, so it will just store the PK of the parent as the id of the child in the proxy. Obviously, you can't use two independent id generators for both of the entities, otherwise PKs may be different. This is the best approach if it meets your requirements.Map parent entity to a database view that joins parent table with the child table and contains all parent columns plus id of child table:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "", insertable = false, updatable = false)
private Child child;