spring-boot - JPA 在 api 响应中为休眠映射返回 null
问题描述
tbl_user_content 表存储国家 ID。在我的回复中,我显示了 tbl_country 表中的国家名称,
- 实体-> 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;
}
- 实体 -> 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
}
请任何人建议
解决方案
推荐阅读
- c# - 添加新对象,创建表单
- java - GWT 中以下 Java API 的等价物是什么?
- android - 按下 webView 中的链接时重定向到另一个活动
- google-chrome - 'chromedriver' 可执行文件需要在 PATH 中
- python - opencv cv2.calibrateCamera 错误
- java - GridBagLayout 控制 Swing 中的对齐方式
- python - 处理数据后如何从 CSV 文件打印到控制台?
- docker - 如何创建子域以公开在 Jenkins 管道中运行到 docker 容器中的应用程序
- c# - 打开 Tally ERP 9
- python - 如何在 Ubuntu 中运行 Anaconda 提示