This package provides a generic and reusable implementation of the Nested Set Model for managing hierarchical data using Spring Boot and JPA.
It is designed to be extended and adapted for any entity that implements the INestedSetNode<ID, T extends INestedSetNode<ID, T>>
interface.
Package: com.mewebstudio.springboot.jpa.nestedset
-
INestedSetNode<ID, T extends INestedSetNode<ID, T>>
Interface that defines the structure of a nested set node (left, right, parent). -
INestedSetNodeResponse<ID>
Interface for representing nodes with children, used for building hierarchical responses. -
JpaNestedSetRepository<T extends INestedSetNode<ID, T>, ID> extends JpaRepository<T, ID>
Base JPA repository interface with custom queries for nested set operations (e.g. find ancestors, descendants, siblings). -
AbstractNestedSetService<T extends INestedSetNode<ID, T>, ID>
Abstract service class that implements common logic for creating, moving, and restructuring nodes in a nested set tree.
- Create new nodes in the correct position in the nested set.
- Move nodes up or down among siblings.
- Retrieve ancestors and descendants of a node.
- Rebuild the entire tree from an unordered list of nodes.
- Shift and close gaps in the tree on node deletion.
- Generic and type-safe structure, reusable across multiple domain entities.
Add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.mewebstudio</groupId>
<artifactId>spring-boot-jpa-nested-set</artifactId>
<version>0.1.2</version>
</dependency>
Add the following dependency to your build.gradle
file:
implementation 'com.mewebstudio:spring-boot-jpa-nested-set:0.1.2'
@Entity
public class Category extends AbstractBaseEntity implements INestedSetNode<String, Category> {
// implement getId, getLeft, getRight, getParent, etc.
}
2. Example repository interface JpaNestedSetRepository<T extends INestedSetNode<ID, T>, ID> extends JpaRepository<T, ID>
public interface CategoryRepository extends JpaNestedSetRepository<Category, String> {
}
// Example service class
@Service
public class CategoryService extends AbstractNestedSetService<Category, String> {
private final CategoryRepository categoryRepository;
public CategoryService(CategoryRepository categoryRepository) {
super(categoryRepository);
this.categoryRepository = categoryRepository;
}
// ...
}
Spring Boot JPA Nested Set (Kotlin Maven Package)