Guest User

Untitled

a guest
Oct 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. @Entity
  2. public class Parent {
  3. @Id
  4. @GeneratedValue
  5. private Long id;
  6.  
  7. @column
  8. private String parentAttribute;
  9.  
  10. @OneToOne(mappedBy = "parent", optional = false)
  11. @JsonBackReference
  12. private Child child;
  13.  
  14. @Entity
  15. public class Child {
  16. @Id
  17. @GeneratedValue
  18. private Long id;
  19.  
  20. @column
  21. private String childAttribute;
  22.  
  23. @OneToOne(optional = false, cascade = CascadeType.ALL)
  24. @JsonManagedReference
  25. private Parent parent;
  26.  
  27. public interface ParentRepository extends CrudRepository<Parent> {
  28.  
  29. @EntityGraph(attributePaths = { "child" })
  30. //a hack to use findAll with default lazy/eager mapping
  31. Collection<Parent> findByIdNotNull();
  32. }
  33.  
  34. Hibernate:
  35. select
  36. parent0_.id as id1_33_0_,
  37. child1_.id as id1_32_1_,
  38. parent0_.parent_attribute as parent_attribute2_33_0_,
  39. child1_.child_attribute as child_attribute2_32_1_,
  40. from
  41. test.parent parent0_
  42. left outer join
  43. test.child child1_
  44. on parent0_.id=child1_.parent_id
  45. where
  46. parent0_.id is not null
  47.  
  48. [ {
  49. "id": 1
  50. "parentAttribute": "I am the parent"
  51. } ]
Add Comment
Please, Sign In to add comment