Last active
April 7, 2021 04:46
-
-
Save rponte/5212789 to your computer and use it in GitHub Desktop.
Handling Hibernate (JPA) lazy association mapping when using @NotFound
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Parent { | |
@Id | |
private Long id; | |
@OneToOne(fetch = FetchType.LAZY) | |
@NotFound(action=NotFoundAction.IGNORE) // You don't need this annotation if you use the approach below | |
private Son son; | |
public Son getSon() { | |
if(!Hibernate.isInitialized(son)) { | |
try { | |
Hibernate.initialize(son); | |
} catch(org.hibernate.ObjectNotFoundException one) { | |
son = null; | |
} | |
} | |
return son; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How i define @NotFound in Hibernate XML mapping?