Interface JpaNestedSetRepository<T extends INestedSetNode<ID>,ID>
- Type Parameters:
T- Type of the nested set node.ID- Type of the identifier for the nested set node.
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<T,,ID> org.springframework.data.jpa.repository.JpaRepository<T,,ID> org.springframework.data.repository.ListCrudRepository<T,,ID> org.springframework.data.repository.ListPagingAndSortingRepository<T,,ID> org.springframework.data.repository.PagingAndSortingRepository<T,,ID> org.springframework.data.repository.query.QueryByExampleExecutor<T>,org.springframework.data.repository.Repository<T,ID>
@NoRepositoryBean
public interface JpaNestedSetRepository<T extends INestedSetNode<ID>,ID>
extends org.springframework.data.jpa.repository.JpaRepository<T,ID>
Repository interface for managing nested set nodes.
This interface extends JpaRepository and provides additional methods for nested set operations.
-
Method Summary
Modifier and TypeMethodDescriptionFind all nodes in the tree, ordered by their left value.findAncestors(int left, int right) Find all ancestors of a given node.findByLeft(int left) Find a node by its left value.findByLeftAndRight(int left, int right) Find a node by its left and right values.findByLeftBetween(int left, int right) Find all nodes with left value between the specified range.findByParentId(ID parentId) Find all nodes that are children of a given parent node.findByRight(int right) Find a node by its right value.findChildren(ID parentId) Find all children of a given parent node.findContaining(int left, int right) Find all the nodes (top level nodes) that cover the given range.findDescendants(int left, int right) Find all descendants of a given node.findExact(int left, int right) Find all the node that exactly matches the given left and right values.findExactExcluding(int left, int right) Retrieves all ancestor nodes of a given node based on its left and right values, excluding the node itself.Find all leaf nodes in the tree.findNextSibling(ID parentId, int right) Find next sibling of a node by its parentId and left value.findNodesToShift(int right) Retrieves nodes with a right value greater than the specified value.findPrevSibling(ID parentId, int left) Find previous sibling of a node by its parentId and left value.Find all root nodes in the tree.findSiblings(ID parentId, ID selfId) Find all siblings of a given node.findSubtree(int left, int right) Find all subtrees of a regular node, including its subtrees.Lock a node by its ID.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findAllOrderedByLeft
Find all nodes in the tree, ordered by their left value. This method retrieves all nodes in the tree structure.- Returns:
- List A list of all nodes ordered by their left value.
-
findRootNodes
@Query("SELECT e FROM #{#entityName} e WHERE e.parent IS NULL ORDER BY e.left") List<T> findRootNodes()Find all root nodes in the tree. This method retrieves nodes where the parent is null.- Returns:
- List A list of all root nodes ordered by their left value.
-
findLeafNodes
Find all leaf nodes in the tree. Retrieves nodes where right = left + 1, meaning they have no children and are leaf nodes.- Returns:
- List A list of all leaf nodes.
-
findPrevSibling
@Query(" SELECT e FROM #{#entityName} e\n WHERE e.right < :left\n AND (\n (:parentId IS NULL AND e.parent IS NULL)\n OR (e.parent.id = :parentId)\n )\n ORDER BY e.left DESC LIMIT 1\n") Optional<T> findPrevSibling(@Param("parentId") ID parentId, @Param("left") int left) Find previous sibling of a node by its parentId and left value.- Parameters:
parentId- ID The ID of the parent node. If null, it searches for root nodes.left- int The left value of the node to find the previous sibling for.- Returns:
- Optional An optional containing the previous sibling node if found, otherwise empty.
-
findNextSibling
@Query(" SELECT e FROM #{#entityName} e\n WHERE e.left > :right\n AND (\n (:parentId IS NULL AND e.parent IS NULL)\n OR (e.parent.id = :parentId)\n )\n ORDER BY e.left ASC LIMIT 1\n") Optional<T> findNextSibling(@Param("parentId") ID parentId, @Param("right") int right) Find next sibling of a node by its parentId and left value.- Parameters:
parentId- ID The ID of the parent node. If null, it searches for root nodes.right- int The right value of the node to find the next sibling for.- Returns:
- Optional An optional containing the next sibling node if found, otherwise empty.
-
findChildren
@Query(" SELECT e FROM #{#entityName} e\n WHERE (:parentId IS NULL AND e.parent IS NULL)\n OR (:parentId IS NOT NULL AND e.parent.id = :parentId)\n ORDER BY e.left\n") List<T> findChildren(@Param("parentId") ID parentId) Find all children of a given parent node. This method retrieves nodes where the parent ID matches the provided parentId.- Parameters:
parentId- ID The ID of the parent node. If null, it searches for root nodes.- Returns:
- List A list of all child nodes ordered by their left value.
-
findSiblings
@Query("SELECT e FROM #{#entityName} e WHERE e.parent.id = :parentId AND e.id <> :selfId ORDER BY e.left") List<T> findSiblings(@Param("parentId") ID parentId, @Param("selfId") ID selfId) Find all siblings of a given node. This method retrieves nodes where the parent ID matches the provided parentId and the node ID is not equal to the provided selfId. This is used to find nodes that share the same parent.- Parameters:
parentId- ID The ID of the parent node.selfId- ID The ID of the node itself. This is used to exclude the node from the results.- Returns:
- List A list of all sibling nodes ordered by their left value.
-
findAncestors
@Query("SELECT e FROM #{#entityName} e WHERE e.left < :left AND e.right > :right ORDER BY e.left DESC") List<T> findAncestors(@Param("left") int left, @Param("right") int right) Find all ancestors of a given node. This method retrieves nodes where the left value is less than the provided left value and the right value is greater than the provided right value.- Parameters:
left- int The left value of the node.right- int The right value of the node.- Returns:
- List A list of all ancestor nodes ordered by their left value.
-
findDescendants
@Query("SELECT e FROM #{#entityName} e WHERE e.left > :left AND e.right < :right ORDER BY e.left") List<T> findDescendants(@Param("left") int left, @Param("right") int right) Find all descendants of a given node. This method retrieves nodes where the left value is greater than the provided left value and the right value is less than the provided right value.- Parameters:
left- int The left value of the node.right- int The right value of the node.- Returns:
- List A list of all descendant nodes ordered by their left value.
-
findSubtree
@Query("SELECT e FROM #{#entityName} e WHERE e.left >= :left AND e.right <= :right ORDER BY e.left ASC") List<T> findSubtree(@Param("left") int left, @Param("right") int right) Find all subtrees of a regular node, including its subtrees. This method retrieves nodes where the left value is greater than or equal to the provided left value and the right value is less than or equal to the provided right value.- Parameters:
left- int The left value of the node.right- int The right value of the node.- Returns:
- List A list of all subtree nodes ordered by their left value.
-
findContaining
@Query("SELECT e FROM #{#entityName} e WHERE e.left <= :left AND e.right >= :right ORDER BY e.left") List<T> findContaining(@Param("left") int left, @Param("right") int right) Find all the nodes (top level nodes) that cover the given range. This method retrieves nodes where the left value is less than or equal to the provided left value and the right value is greater than or equal to the provided right value.- Parameters:
left- int The left value of the range.right- int The right value of the range.- Returns:
- List A list of all nodes that cover the specified range ordered by their left value.
-
findExact
@Query("SELECT e FROM #{#entityName} e WHERE e.left = :left AND e.right = :right ORDER BY e.left") List<T> findExact(@Param("left") int left, @Param("right") int right) Find all the node that exactly matches the given left and right values. This method retrieves nodes where the left value is equal to the provided left value and the right value is equal to the provided right value.- Parameters:
left- int The left value of the node.right- int The right value of the node.- Returns:
- List A list of all nodes that exactly match the specified left and right values ordered by their left value.
-
findExactExcluding
@Query("SELECT e FROM #{#entityName} e WHERE e.left < :left AND e.right > :right ORDER BY e.left") List<T> findExactExcluding(@Param("left") int left, @Param("right") int right) Retrieves all ancestor nodes of a given node based on its left and right values, excluding the node itself. This method retrieves nodes where the left value is less than the provided left value and the right value is greater than the provided right value.- Parameters:
left- int The left value of the node.right- int The right value of the node.- Returns:
- List A list of all ancestor nodes excluding the node itself ordered by their left value.
-
findNodesToShift
@Query("SELECT c FROM #{#entityName} c WHERE c.right > :right") List<T> findNodesToShift(@Param("right") int right) Retrieves nodes with a right value greater than the specified value. Used when shifting nodes during insertion or deletion to maintain nested set integrity. This method retrieves nodes where the right value is greater than the provided right value. This is used to find nodes that need to be shifted when inserting a new node.- Parameters:
right- int The right value to compare against.- Returns:
- List A list of all nodes that need to be shifted ordered by their left value.
-
findByLeft
Find a node by its left value. This method retrieves a single node where the left value matches the provided left value.- Parameters:
left- int The left value of the node to find.- Returns:
- Optional An optional containing the node if found, otherwise empty.
-
findByRight
Find a node by its right value. This method retrieves a single node where the right value matches the provided right value.- Parameters:
right- int The right value of the node to find.- Returns:
- Optional An optional containing the node if found, otherwise empty.
-
findByLeftAndRight
Find a node by its left and right values. This method retrieves a single node where the left and right values match the provided values.- Parameters:
left- int The left value of the node to find.right- int The right value of the node to find.- Returns:
- Optional An optional containing the node if found, otherwise empty.
-
findByParentId
@Query("SELECT c FROM #{#entityName} c WHERE c.parent.id = :parentId ORDER BY c.left") List<T> findByParentId(@Param("parentId") ID parentId) Find all nodes that are children of a given parent node. This method retrieves nodes where the parent ID matches the provided parentId.- Parameters:
parentId- ID The ID of the parent node.- Returns:
- List A list of all child nodes ordered by their left value.
-
findByLeftBetween
@Query("SELECT e FROM #{#entityName} e WHERE e.left BETWEEN :left AND :right ORDER BY e.left") List<T> findByLeftBetween(@Param("left") int left, @Param("right") int right) Find all nodes with left value between the specified range. This is useful when moving nodes within the nested set.- Parameters:
left- int The left value of the range.right- int The right value of the range.- Returns:
- List A list of all nodes with left value between the specified range ordered by their left value.
-
lockNode
@Query("SELECT c FROM #{#entityName} c WHERE c.id = :id") @Lock(PESSIMISTIC_WRITE) Optional<T> lockNode(@Param("id") ID id) Lock a node by its ID. Prevents race conditions by locking a node with a pessimistic write lock. Especially useful for nested set manipulations. This method retrieves a single node where the ID matches the provided ID and locks it for writing.- Parameters:
id- ID The ID of the node to lock.- Returns:
- Optional An optional containing the locked node if found, otherwise empty.
-