Hello, I have an entity class @Entity @Table(name = employees) public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne(fetch
Hello,
I have an entity class
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "manager_id")
private Employee manager;
// getters and setters
}
It has a self referencing (hierarchal)
and I have a repository to return the parents(managers) and the children(employees) under them.
public interface EmployeeRepository extends JpaRepository
@Query(nativeQuery = true,
value = "WITH RECURSIVE manager_tree(id, name) AS (" +
" SELECT id, name " +
" FROM employees " +
" WHERE manager_id IS NULL " +
"UNION ALL " +
" SELECT e.id, e.name " +
" FROM employees e " +
" JOIN manager_tree mt ON mt.id = e.manager_id " +
") " +
"SELECT * FROM manager_tree")
List
}
I want this query to be written using Spring JPA (native queries are not allowed), so could any expert help me by rewrite the query using Spring JPA (native queries are not allowed)
and the manager of the managers their id is 1 not null (WHERE manager_id IS NULL) so I hope the expert change this line also.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started