Interface JpaNestedSetRepository
-
- All Implemented Interfaces:
-
org.springframework.data.jpa.repository.JpaRepository
,org.springframework.data.repository.CrudRepository
,org.springframework.data.repository.ListCrudRepository
,org.springframework.data.repository.ListPagingAndSortingRepository
,org.springframework.data.repository.PagingAndSortingRepository
,org.springframework.data.repository.Repository
,org.springframework.data.repository.query.QueryByExampleExecutor
@NoRepositoryBean() public interface JpaNestedSetRepository<T extends INestedSetNode<ID, T>, ID extends Object> implements 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 Type Method Description abstract List<T>
findAllOrderedByLeft()
Find all nodes in the tree, ordered by their left value. abstract List<T>
findRootNodes()
Find all root nodes in the tree. abstract List<T>
findLeafNodes()
Find all leaf nodes in the tree. abstract T
findPrevSibling(@Param(value = "parentId") ID parentId, @Param(value = "left") Integer left)
Find previous sibling of a node by its parentId and left value. abstract T
findNextSibling(@Param(value = "parentId") ID parentId, @Param(value = "right") Integer right)
Find next sibling of a node by its parentId and left value. abstract List<T>
findChildren(@Param(value = "parentId") ID parentId)
Find all children of a given parent node. abstract List<T>
findSiblings(@Param(value = "parentId") ID parentId, @Param(value = "selfId") ID selfId)
Find all siblings of a given node. abstract List<T>
findAncestors(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all ancestors of a given node. abstract List<T>
findDescendants(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all descendants of a given node. abstract List<T>
findSubtree(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all subtrees of a regular node, including its subtrees. abstract List<T>
findContaining(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all the nodes (top level nodes) that cover the given range. abstract List<T>
findExact(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all the node that exactly matches the given left and right values. abstract List<T>
findExactExcluding(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Retrieves all ancestor nodes of a given node based on its left and right values, excluding the node itself. abstract List<T>
findNodesToShift(@Param(value = "right") Integer right)
Retrieves nodes with a right value greater than the specified value. abstract T
findByLeft(Integer left)
Find a node by its left value. abstract T
findByRight(Integer right)
Find a node by its right value. abstract T
findByLeftAndRight(Integer left, Integer right)
Find a node by its left and right values. abstract List<T>
findByParentId(@Param(value = "parentId") ID parentId)
Find all nodes that are children of a given parent node. abstract List<T>
findByLeftBetween(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all nodes with left value between the specified range. abstract T
lockNode(@Param(value = "id") ID id)
Lock a node by its ID. -
Methods inherited from class org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, save
-
Methods inherited from class org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAll
-
Methods inherited from class org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlush
-
Methods inherited from class org.springframework.data.repository.ListPagingAndSortingRepository
findAll
-
Methods inherited from class org.springframework.data.repository.PagingAndSortingRepository
findAll
-
Methods inherited from class org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
findAllOrderedByLeft
@Query(value = "SELECT e FROM #{#entityName} e ORDER BY e.left") abstract List<T> findAllOrderedByLeft()
Find all nodes in the tree, ordered by their left value. This method retrieves all nodes in the tree structure.
- Returns:
A list of all nodes ordered by their left value.
-
findRootNodes
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.parent IS NULL ORDER BY e.left") abstract List<T> findRootNodes()
Find all root nodes in the tree. This method retrieves nodes where the parent is null.
- Returns:
A list of all root nodes ordered by their left value.
-
findLeafNodes
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left + 1 = e.right") abstract List<T> findLeafNodes()
Find all leaf nodes in the tree. Retrieves nodes where right = left + 1, meaning they have no children and are leaf nodes.
- Returns:
A list of all leaf nodes.
-
findPrevSibling
@Query(value = " SELECT e FROM #{#entityName} e WHERE e.right < :left AND ( (:parentId IS NULL AND e.parent IS NULL) OR (e.parent.id = :parentId) ) ORDER BY e.left DESC LIMIT 1 ") abstract T findPrevSibling(@Param(value = "parentId") ID parentId, @Param(value = "left") Integer left)
Find previous sibling of a node by its parentId and left value.
- Parameters:
parentId
- The ID of the parent node.left
- The left value of the node to find the previous sibling for.- Returns:
An optional containing the previous sibling node if found, otherwise empty.
-
findNextSibling
@Query(value = " SELECT e FROM #{#entityName} e WHERE e.left > :right AND ( (:parentId IS NULL AND e.parent IS NULL) OR (e.parent.id = :parentId) ) ORDER BY e.left ASC LIMIT 1 ") abstract T findNextSibling(@Param(value = "parentId") ID parentId, @Param(value = "right") Integer right)
Find next sibling of a node by its parentId and left value.
- Parameters:
parentId
- The ID of the parent node.right
- The right value of the node to find the next sibling for.- Returns:
An optional containing the next sibling node if found, otherwise empty.
-
findChildren
@Query(value = " SELECT e FROM #{#entityName} e WHERE (:parentId IS NULL AND e.parent IS NULL) OR (:parentId IS NOT NULL AND e.parent.id = :parentId) ORDER BY e.left ") abstract List<T> findChildren(@Param(value = "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
- The ID of the parent node.- Returns:
A list of all child nodes ordered by their left value.
-
findSiblings
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.parent.id = :parentId AND e.id <> :selfId ORDER BY e.left") abstract List<T> findSiblings(@Param(value = "parentId") ID parentId, @Param(value = "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
- The ID of the parent node.selfId
- The ID of the node itself.- Returns:
A list of all sibling nodes ordered by their left value.
-
findAncestors
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left < :left AND e.right > :right ORDER BY e.left DESC") abstract List<T> findAncestors(@Param(value = "left") Integer left, @Param(value = "right") Integer 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
- The left value of the node.right
- The right value of the node.- Returns:
A list of all ancestor nodes ordered by their left value.
-
findDescendants
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left > :left AND e.right < :right ORDER BY e.left") abstract List<T> findDescendants(@Param(value = "left") Integer left, @Param(value = "right") Integer 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
- The left value of the node.right
- The right value of the node.- Returns:
A list of all descendant nodes ordered by their left value.
-
findSubtree
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left >= :left AND e.right <= :right ORDER BY e.left ASC") abstract List<T> findSubtree(@Param(value = "left") Integer left, @Param(value = "right") Integer 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
- The left value of the node.right
- The right value of the node.- Returns:
A list of all subtree nodes ordered by their left value.
-
findContaining
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left <= :left AND e.right >= :right ORDER BY e.left") abstract List<T> findContaining(@Param(value = "left") Integer left, @Param(value = "right") Integer 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
- The left value of the range.right
- The right value of the range.- Returns:
A list of all nodes that cover the specified range ordered by their left value.
-
findExact
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left = :left AND e.right = :right ORDER BY e.left") abstract List<T> findExact(@Param(value = "left") Integer left, @Param(value = "right") Integer 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
- The left value of the node.right
- The right value of the node.- Returns:
A list of all nodes that exactly match the specified left and right values ordered by their left value.
-
findExactExcluding
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left < :left AND e.right > :right ORDER BY e.left") abstract List<T> findExactExcluding(@Param(value = "left") Integer left, @Param(value = "right") Integer 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
- The left value of the node.right
- The right value of the node.- Returns:
A list of all ancestor nodes excluding the node itself ordered by their left value.
-
findNodesToShift
@Query(value = "SELECT c FROM #{#entityName} c WHERE c.right > :right") abstract List<T> findNodesToShift(@Param(value = "right") Integer 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
- The right value to compare against.- Returns:
A list of all nodes that need to be shifted ordered by their left value.
-
findByLeft
abstract T findByLeft(Integer left)
Find a node by its left value. This method retrieves a single node where the left value matches the provided left value.
- Parameters:
left
- The left value of the node to find.- Returns:
An optional containing the node if found, otherwise empty.
-
findByRight
abstract T findByRight(Integer right)
Find a node by its right value. This method retrieves a single node where the right value matches the provided right value.
- Parameters:
right
- The right value of the node to find.- Returns:
An optional containing the node if found, otherwise empty.
-
findByLeftAndRight
abstract T findByLeftAndRight(Integer left, Integer right)
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
- The left value of the node to find.right
- The right value of the node to find.- Returns:
An optional containing the node if found, otherwise empty.
-
findByParentId
@Query(value = "SELECT c FROM #{#entityName} c WHERE c.parent.id = :parentId ORDER BY c.left") abstract List<T> findByParentId(@Param(value = "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
- The ID of the parent node.- Returns:
A list of all child nodes ordered by their left value.
-
findByLeftBetween
@Query(value = "SELECT e FROM #{#entityName} e WHERE e.left BETWEEN :left AND :right ORDER BY e.left") abstract List<T> findByLeftBetween(@Param(value = "left") Integer left, @Param(value = "right") Integer right)
Find all nodes with left value between the specified range. This is useful when moving nodes within the nested set.
- Parameters:
left
- The left value of the range.right
- The right value of the range.- Returns:
A list of all nodes with left value between the specified range ordered by their left value.
-
lockNode
@Query(value = "SELECT c FROM #{#entityName} c WHERE c.id = :id")@Lock(value = LockModeType.PESSIMISTIC_WRITE) abstract T lockNode(@Param(value = "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
- The ID of the node to lock.- Returns:
An optional containing the locked node if found, otherwise empty.
-
-
-
-