Проблема отношения класса не возвращает некоторые данные в JSON - PullRequest
0 голосов
/ 26 апреля 2020

Мне нужно вернуть Json в мой проект:

{
  "data": {
    "id": 1,
    "username": "renato",
    "name": "Renato",
    "email": "asdasd@outlook.com",
    "roles": [
      {
        "id": 1,
        "name": "ROLE_USER",
        "accessList": [
          {
            "id_access": 1,
            "id_role": {
              "id_role": 1,
              "name": "ROLE_USER",
              "authority": "ROLE_USER"
            },
            "id_program": {
              "id_program": 1,
              "code_program": "TEST",
              "name": "test"
             },
            "id_view": {
              "id_view": 1,
              "code_view": "TEST",
              "name": "test"
             },
            "menuYesNo": true,
            "accessYesNo": true,
            "saveYesNo": true,
            "editYesNo": true,
            "deleteYesNo": true
          }
        ]
      }
    ]
  }
}

Но он вернет это:

{
  "data": {
    "id": 1,
    "username": "renato",
    "name": "Renato",
    "email": "asdasd@outlook.com",
    "roles": [
      {
        "id": 1,
        "name": "ROLE_USER",
        "accessList": [
          {
            "id_access": 1,
            "id_role": {
              "id_role": 1,
              "name": "ROLE_USER",
              "authority": "ROLE_USER"
            },
            "id_program": {},
            "id_view": {},
            "menuYesNo": true,
            "accessYesNo": true,
            "saveYesNo": true,
            "editYesNo": true,
            "deleteYesNo": true
          } 
        ]
      }
    ]
  }
}

Только классы AccessModel и RoleModel имеют двунаправленную связь, существует однонаправленная связь между ProgramModel и ViewModel с AccessModel.

OBS: я сопоставил UserModel с UserDTO, используя ModelMapper. Существует RoleModel внутри UserDTO. RoleModel и AccessModel имеют @JsonManagedReference и @JsonBackReference соответственно, но ProgramModel и ViewModel нет.

@Data
@Entity
@Table(schema = "`SCH`", name = "`USER`")
public class UserModel implements UserDetails {

    private static final long serialVersionUID = -2195101536379303067L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "USER", name="`ID_USER`", nullable = true)
    private Long id_user;
    @Column(table = "USER", name="`USERNAME`", nullable = true, length = 50)
    private String username;
    @Column(table = "USER", name="`PASSWORD`", nullable = true, length = 255)
    private String password;
    @Column(table = "USER", name="`NAME`", length = 255)
    private String name;
    @Column(table = "USER", name="`EMAIL`", length = 255)
    private String email;
    @Column(table = "USER", name="`DATE_EXPERED`", nullable = true)
    private LocalDate dateExpered;
    @Column(table = "USER", name="`ACCOUNT_ACTIVE`", nullable = true)
    private Boolean accountAtive;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(schema = "`SCH`", name = "`USER_ROLE`" ,
    joinColumns = @JoinColumn(
        name = "`CD_USER`", referencedColumnName ="`ID_USER`"
    ),
    inverseJoinColumns = @JoinColumn(
        name = "`CD_ROLE`", referencedColumnName = "`ID_ROLE`"
    ))
    @JsonBackReference
    private Collection<RoleModel> roles;

    //METHODS USERDETAILS
}

@Data
@Entity
@Table(schema = "`SCH`", name = "`ROLE`")
public class RoleModel implements GrantedAuthority {

    private static final long serialVersionUID = -1320143054659054908L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "ROLE", name = "`ID_ROLE`", nullable = true)
    private Long id_role;

    @Column(table = "ROLE", name = "`NAME`", nullable = true, length = 255)
    private String name;

    @JsonBackReference
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "id_role", fetch = FetchType.LAZY)
    private List<AccessModel> accessList;

}

@Data
@Entity
@Table(schema = "`SCH`", name = "`ACCESS`")
public class AccessModel implements Serializable {

    private static final long serialVersionUID = -5590889002302223720L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "ACCESS", name = "`ID_ACCESS`", nullable = true)
    private Long id_access;


    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`CD_ROLE`")
    @JsonManagedReference
    private RoleModel id_role;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`CD_PROGRAM`")
    private ProgramModel id_program;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "`CD_VIEW`")
    private ViewModel id_view;

    @Column(table = "ACCESS", name = "`MENU_YES_NO`", nullable = true)
    private Boolean menuYesNo;

    @Column(table = "ACCESS", name = "`ACCESS_YES_NO`", nullable = true)
    private Boolean accessYesNo;

    @Column(table = "ACCESS", name = "`SAVE_YES_NO`", nullable = true)
    private Boolean saveYesNo;

    @Column(table = "ACCESS", name = "`EDIT_YES_NO`", nullable = true)
    private Boolean editYesNo;

    @Column(table = "ACCESS", name = "`DELETE_YES_NO`", nullable = true)
    private Boolean deleteYesNo;
}

@Entity
@Table(name = "`PROGRAM`", schema = "`SCH`")
public class ProgramModel implements Serializable {

    private static final long serialVersionUID = -726159076909575803L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "PROGRAM", name = "`ID_PROGRAM`", nullable = true)
    private Long id_program;

    @Column(table = "PROGRAM", name = "`CODE_PROGRAM`", nullable = true)
    private String code_program;

    @Column(table = "PROGRAM", name = "`NAME`", nullable = true)
    private String name;

    @Column(table = "PROGRAM", name = "`ACTIVE`", nullable = true)
    private Boolean active;

}

@Entity
@Table(name = "`VIEW`", schema = "`SCH`")
public class ViewModel implements Serializable {

    private static final long serialVersionUID = 3900486010030569933L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(table = "VIEW", name = "`ID_VIEW`", nullable = true)
    private Long id_view;
    @Column(table = "VIEW", name = "`CODE_VIEW`", nullable = true)
    private String code_view;
    @Column(table = "VIEW", name = "`NAME`", nullable = true)
    private String name;
    @Column(table = "VIEW", name = "`ACTIVE`", nullable = true)
    private Boolean active;

}

1 Ответ

0 голосов
/ 27 апреля 2020

Я забыл поставить @Data в ProgramModel и ViewModel.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...