Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- public class Parent {
- @Id
- @GeneratedValue
- private Long id;
- @column
- private String parentAttribute;
- @OneToOne(mappedBy = "parent", optional = false)
- @JsonBackReference
- private Child child;
- @Entity
- public class Child {
- @Id
- @GeneratedValue
- private Long id;
- @column
- private String childAttribute;
- @OneToOne(optional = false, cascade = CascadeType.ALL)
- @JsonManagedReference
- private Parent parent;
- public interface ParentRepository extends CrudRepository<Parent> {
- @EntityGraph(attributePaths = { "child" })
- //a hack to use findAll with default lazy/eager mapping
- Collection<Parent> findByIdNotNull();
- }
- Hibernate:
- select
- parent0_.id as id1_33_0_,
- child1_.id as id1_32_1_,
- parent0_.parent_attribute as parent_attribute2_33_0_,
- child1_.child_attribute as child_attribute2_32_1_,
- from
- test.parent parent0_
- left outer join
- test.child child1_
- on parent0_.id=child1_.parent_id
- where
- parent0_.id is not null
- [ {
- "id": 1
- "parentAttribute": "I am the parent"
- } ]
Add Comment
Please, Sign In to add comment