如何在SpringJPA中正确Map4个表

kknvjkwl  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(228)

我有四张table
区域:它是一个目录,其中包含组织(父级)的所有部门
子区域:它是一个目录,其中在目录(区域),即子目录之前,所有子区域都属于该目录
个人:组织所有员工所在的目录
personal area(个人区域):这是一个表,其中包含前面描述的表之间的所有关系
业务逻辑很简单,员工可以属于多个区域中的一个,每个区域只能属于一个子区域。
这是我的关系

这些是我的实体
个人的

@Entity
@Table(name = "personal", catalog = "almacen", schema = "")
public class Personal {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 30)
    @Column(name = "curp")
    private String curp;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 200)
    @Column(name = "nombre")
    private String nombre;
    @JsonIgnore
    @OneToMany(mappedBy = "personal")
    private Set<PersonalArea> registrations = new HashSet<>();

}

个人区域

@Getter
@Setter
@Entity
@Table(name = "personal_area", catalog = "almacen", schema = "")
@XmlRootElement
public class PersonalArea {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private Long id;
    @ManyToOne
    @JoinColumn(name = "personal_id")

    private Personal personal;
    @ManyToOne
    @JoinColumn(name = "area_id")
    private Area area;

    @ManyToOne
    @JoinColumn(name = "subarea_id")
    private subArea subArea;

    private Date fechaCreacion;

}

地区

@Getter
@Setter
@Entity
@Table(name = "area", catalog = "almacen", schema = "")
@XmlRootElement
public class Area {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private Long id;
    private String nombreArea;
    @JsonIgnore

    @OneToMany(mappedBy = "area")
    private Set<PersonalArea> registrations = new HashSet<>();

}

分区

@Getter
@Setter
@Entity
@Table(name = "sub_area", catalog = "almacen", schema = "")
public class subArea {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private Long id;
    private Long idArea;
    private String nombre;

    @JsonIgnore
    @OneToMany(mappedBy = "personal")
    private Set<PersonalArea> registrations = new HashSet<>();
}

事实上,我得到了这样的东西,正如你所看到的,json有两倍于同一个人,他属于两个区域,每个区域一个子区域。。。没关系,但我不想要这个

{
    "items": [{
            "id": 1,
            "personal": {
                "id": 1,
                "curp": "AEMM680119HHGRRR03",
                "nombre": "MARIO RUBIZEL",
                "primerApellido": "ARTEAGA",
                "segundoApellido": "MORALES",
                "telefono": "7711263949",
                "fechaCreacion": null,
                "fechaModificacion": null
            },
            "area": {
                "id": 2,
                "nombreArea": "PERSONAL ADMINISTRATIVO"
            },
            "subArea": {
                "id": 1,
                "idArea": 2,
                "nombre": "CONTROL ESCOLAR"
            },
            "fechaCreacion": "2021-06-24T16:26:49.000+00:00"
        },
        {
            "id": 2,
            "personal": {
                "id": 1,
                "curp": "AEMM680119HHGRRR03",
                "nombre": "MARIO RUBIZEL",
                "primerApellido": "ARTEAGA",
                "segundoApellido": "MORALES",
                "telefono": "7711263949",
                "fechaCreacion": null,
                "fechaModificacion": null
            },
            "area": {
                "id": 5,
                "nombreArea": "PERSONAL EXTERNO"
            },
            "subArea": {
                "id": 2,
                "idArea": 5,
                "nombre": "FOTOCOPIADO"
            },
            "fechaCreacion": "2021-06-24T16:26:49.000+00:00"
        }
    ]
}

我需要像这样的json:像孩子一样只显示一次个人区域和y分区

{
        "items": [{
                "id": 1,
                "personal": {
                    "id": 1,
                    "curp": "AEMM680119HHGRRR03",
                    "nombre": "MARIO RUBIZEL",
                    "primerApellido": "ARTEAGA",
                    "segundoApellido": "MORALES",
                    "telefono": "7711263949",
                    "fechaCreacion": null,
                    "fechaModificacion": null
                },
                "area": [{
                        "id": 1,
                        "nombreArea": "DIRECTOR",
                        "subArea": {
                            "id": 1,
                            "idArea": 2,
                            "nombre": "CONTROL ESCOLAR"
                        }
                    },
                    {
                        "id": 5,
                        "nombreArea": "PERSONAL EXTERNO",
                        "subArea": {
                            "id": 2,
                            "idArea": 5,
                            "nombre": "FOTOCOPIADO"
                        }
                    }

                ],
                "fechaCreacion": "2021-06-24T16:26:49.000+00:00"
            }

        ]
    }

提前谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题