How To Create Dynamic Menu from Database with Spring Boot

Viewed 289

I have been trying to load the menu from a database using Spring JPA and Spring Boot with a SQL Server database.

Here is my database structure

enter image description here

What I want to achieve is this structure:

[
  {
    "id": "1",
    "menuName": "Claim",
    "moduleName": null,
    "active": true,
    "children": [
      {
        "id": "2",
        "menuName": "Claim OFR Finish",
        "moduleName": "#/claim-ofr-finish",
        "active": true,
        "children": []
      },
      {
        "id": "3",
        "menuName": "OFR Holiday",
        "moduleName": "#/ofr-holiday",
        "active": true,
        "children": []
      }
    ]
  }
]

But instead of achieving that, i get this output:

[
  {
    "id": "1",
    "menuName": "Claim",
    "moduleName": null,
    "active": true,
    "children": [
      {
        "id": "2",
        "menuName": "Claim OFR Finish",
        "moduleName": "#/claim-ofr-finish",
        "active": true,
        "children": []
      },
      {
        "id": "3",
        "menuName": "OFR Holiday",
        "moduleName": "#/ofr-holiday",
        "active": true,
        "children": []
      }
    ]
  },
  {
    "id": "2",
    "menuName": "Claim OFR Finish",
    "moduleName": "#/claim-ofr-finish",
    "active": true,
    "children": []
  },
  {
    "id": "3",
    "menuName": "OFR Holiday",
    "moduleName": "#/ofr-holiday",
    "active": true,
    "children": []
  }
]

Here is my entity class:

@Entity
@Table(name = "menu_tbl")
public class UserMenu implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    private String id;
    
    @Column(name = "menu_name")
    private String menuName;
    
    @Column(name = "module_name")
    private String moduleName;
    
    @Column(name = "active")
    private Boolean active;
    
    @JsonManagedReference
    @OneToMany(cascade = CascadeType.ALL, fetch= FetchType.EAGER)
    @JoinColumn(name = "parent_id")
    @OrderBy("id ASC")
    private Set<UserMenu> children;
     
    @JsonBackReference
    @ManyToOne(fetch = FetchType.LAZY)
    private UserMenu parent;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMenuName() {
        return menuName;
    }

    public void setMenuName(String menuName) {
        this.menuName = menuName;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Set<UserMenu> getChildren() {
        return children;
    }

    public void setChildren(Set<UserMenu> children) {
        this.children = children;
    }

    public UserMenu getParent() {
        return parent;
    }

    public void setParent(UserMenu parent) {
        this.parent = parent;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    
}

And here is my repository class:

@Repository
public interface UserMenuRepository extends JpaRepository<UserMenu, Long> {
    
    List<UserMenu> findAll();

}

Need help on how to achieve the expected result.

1 Answers

Set does not support duplicate value. use arraylist.

Related