C
C
Chvalov2018-10-14 14:13:17
Hibernate
Chvalov, 2018-10-14 14:13:17

What is the best way to make a multi-level menu in JPA SpringBoot?

It is necessary to make a multi-level menu, something like this should be the result:

- Menu Item 1
 + Submenu Item 1
 + Submenu Item 2
 - Submenu Item 3
    + Sub_Submenu Item 1
    + Sub_Submenu Item 2
- Menu Item 2
 + Submenu Item 1
 + Submenu Item 2

Found several implementations:
Option 1
@Entity
@Table(name = "category" )
public class Category {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    private String name;

    @ManyToOne @JoinColumn(name="parent_id") @JsonIgnore
   // @ColumnDefault("0")
    private Category parentId;
    @OneToMany(mappedBy="parentId")
    private List<Category> subCategories=new ArrayList<>();
}

Option 2
@Entity
@Table(name = "app_category")
public class Category {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name", nullable = false)
    private String name;

    @ManyToOne
    @JoinColumn(name = "parentid")
    private Category parent;

    @OneToMany(mappedBy = "parent")
    private Set<Category> childCategories;

    // https://github.com/Damiox/ecommerce-rest-spring-jpa

Option 3
@Entity
@Table(name = "MENU")
public class Menu {
    @Id
    @Column(name = "ID")
    private int id;

    @Column(name = "NAME")
    private String name;

    @OneToMany
    @JoinColumn(name = "FATHER", referencedColumnName = "ID")
    private List<Menu> childMenu;
}

Another option, more flexible of all https://gist.github.com/michail-nikolaev/3840973
Which one to take as a basis, maybe there will be some more advice?
Spring Boot 2

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question