首页 > 解决方案 > JPA 在 api 响应中为休眠映射返回 null

问题描述

tbl_user_content 表存储国家 ID。在我的回复中,我显示了 tbl_country 表中的国家名称,

  1. 实体-> A
@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@DynamicInsert
@Table(name = "tbl_user_content")
public class ContentManage implements Serializable {

    private static final long serialVersionUID = -2526575458651174224L;

    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "user_content_generator")
    @SequenceGenerator(name= "user_content_generator", sequenceName = "tbl_user_content_id_seq", allocationSize = 1)
    @Column(name ="id")
    private  int id;

    @Column(name = "position")
    private  int position;

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

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

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

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

    @Column(name = "last_updated_date")
    private ZonedDateTime last_updated_date;

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

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "countryid", insertable = false, updatable = false)
    @Fetch(FetchMode.JOIN)
    private Country country;

}

  1. 实体 -> B
@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@DynamicInsert
@Table(name = "tbl_country")
public class Country implements Serializable {

    private static final long serialVersionUID = -2526575458651174224L;

    @Id
    @Column(name ="id")
    private  String countryId;

    @Column(name = "created_dt")
    private ZonedDateTime created_date;

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

    @OneToMany(targetEntity = ContentManage.class, mappedBy = "country", orphanRemoval = false, fetch = FetchType.EAGER)
    private Set<ContentManage> contentManage;

}

服务等级

public ContentManageVO queryContenetManage(int contentmanageId) {
        System.out.println("contentmanageid value==="+contentmanageId);
        Optional<ContentManage> contentManageOptional = contentManageRepository.findById(contentmanageId);
        ContentManage contentManage = contentManageOptional
                .orElseThrow(() -> new GenericBadException(StaffNotificationExceptionEnum.CONTENT_MANAGE_NOT_FOUND_EXCEPTION));
        ContentManageVO contentManageVO = new ContentManageVO();
        BeanUtils.copyProperties(contentManage, contentManageVO);
        return contentManageVO;
    }

API 响应:国家/地区 ID 返回 null

{
"id": 17,
"position": 123,
"title": "title",
"shortDescription": "shortdescription",
"thumbnailimage": "thumbnailimage",
"linkactions": "linkactions",
"last_updated_date": "2021-02-23T14:02:42.891+08",
"last_updated_by": "last_updated_by",
"countryid": null

}

请任何人建议

在调试时我可以找到国家实体具有价值

但响应实体无法设置值

标签: spring-boothibernaterestjpaspring-data-jpa

解决方案


推荐阅读