Advertisement
Guest User

Untitled

a guest
Jun 6th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. @Entity
  2. @Table(name = "human")
  3. @DynamicInsert(value = true)
  4. @DynamicUpdate(value = true)
  5. public class Human {
  6.  
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.AUTO)
  9. private Long id;
  10.  
  11. @Column(nullable = false, name = "name")
  12. private String name;
  13.  
  14. @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
  15. @JoinColumn(name = "parent_id")
  16. private Human parent;
  17.  
  18. @OneToMany(fetch=FetchType.LAZY, mappedBy ="parent")
  19. private Collection<ProductCategory> subcategories;
  20.  
  21. // getters and setters ...
  22. }
  23.  
  24. @Repository
  25. public interface HumanDao extends CrudRepository<Human, Long>{
  26. @Query(value =
  27. "SELECT * " +
  28. "FROM humans " +
  29. "WHERE parent_id = ?1",
  30. nativeQuery = true)
  31. public Iterable<Human> getChildren(Long parentId);
  32.  
  33. }
  34.  
  35. public class TestClass {
  36.  
  37. @Autowired
  38. private HumanDao dao;
  39.  
  40. public void test(Long parentId) {
  41. Iterable<Human> children = dao.getChildren(parentId);
  42. System.out.println(children);
  43. }
  44. }
  45.  
  46. spring.datasource.url=jdbc:mysql://localhost/db
  47. spring.datasource.username=username
  48. spring.datasource.password=password
  49. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  50. spring.jpa.properties.max_fetch_depth=1
  51.  
  52. ID | Name | Parent
  53. 1 | Mike | null
  54. 2 | John | 1
  55. 3 | Bill | 2
  56. 4 | Carl | 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement