Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "human")
- @DynamicInsert(value = true)
- @DynamicUpdate(value = true)
- public class Human {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @Column(nullable = false, name = "name")
- private String name;
- @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
- @JoinColumn(name = "parent_id")
- private Human parent;
- @OneToMany(fetch=FetchType.LAZY, mappedBy ="parent")
- private Collection<ProductCategory> subcategories;
- // getters and setters ...
- }
- @Repository
- public interface HumanDao extends CrudRepository<Human, Long>{
- @Query(value =
- "SELECT * " +
- "FROM humans " +
- "WHERE parent_id = ?1",
- nativeQuery = true)
- public Iterable<Human> getChildren(Long parentId);
- }
- public class TestClass {
- @Autowired
- private HumanDao dao;
- public void test(Long parentId) {
- Iterable<Human> children = dao.getChildren(parentId);
- System.out.println(children);
- }
- }
- spring.datasource.url=jdbc:mysql://localhost/db
- spring.datasource.username=username
- spring.datasource.password=password
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.jpa.properties.max_fetch_depth=1
- ID | Name | Parent
- 1 | Mike | null
- 2 | John | 1
- 3 | Bill | 2
- 4 | Carl | 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement