spring-boot - 休眠延迟初始化对象的 Spring Data Rest HAL 响应格式问题
问题描述
技术栈: Spring Boot (2.3.1.RELEASE)、Spring Data JPA、Sprign Data Rest (3.3.1 RELEASE)、Hibernate (5.4.17 Final)、PostgresQL。我有父实体ProductEntity:
@Data
@Entity
@Table(name = "product")
@EntityListeners(AuditingEntityListener.class)
public class ProductEntity {
@NotNull
@Id
@EqualsAndHashCode.Exclude
@Type(type = "pg-uuid")
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
private UUID id;
@JsonProperty("productType")
@ToString.Exclude
@OneToOne(mappedBy = "productEntity", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, optional = false)
private ProductTypeEntity productTypeEntity;
}
结束子实体ProductTypeEntity:
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Entity
@Table(name = "product_type")
@JsonIgnoreProperties({"hibernateLazyInitializer"})
public class ProductTypeEntity extends BaseEntity implements RootAware<ProductEntity> {
@NotBlank
@Size(max = 255)
private String type;
@NotBlank
@Size(max = 255)
private String brand;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@MapsId
@JoinColumn(name = "id")
@ToString.Exclude
@EqualsAndHashCode.Exclude
@JsonIgnore
private ProductEntity productEntity;
@Override
public ProductEntity root() {
return productEntity;
}
}
最后有一个 Spring Data REST Repository ProductRepository:
@RepositoryRestResource(collectionResourceRel = "products", path = "products")
public interface ProductRepository extends JpaRepository<ProductEntity, UUID> {
}
主要问题是当我请求GET http://localhost:8081/api/v1/products时,我在productType下收到了额外的字段内容,这些内容完全是多余的,并且无法充分解析响应:
{
"_embedded": {
"products": [
{
"productType": {
"content": {
"type": "type1",
"brand": "brand1"
}
}
"_links": {
"self": {
"href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
},
"productEntity": {
"href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/v1/products"
},
"profile": {
"href": "http://localhost:8081/api/v1/profile/products"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
但是当我将 productTypeEntity 从LAZY更改为EAGER时:
@JsonProperty("productType")
@ToString.Exclude
@OneToOne(mappedBy = "productEntity", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, optional = false)
private ProductTypeEntity productTypeEntity;
我收到没有内容字段的正确响应:
{
"_embedded": {
"products": [
{
"productType": {
"type": "type1",
"brand": "brand1",
"_links": {
"productEntity": {
"href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
}
}
},
"_links": {
"self": {
"href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
},
"productEntity": {
"href": "http://localhost:8081/api/v1/products/6b0e375e-4d2d-41af-b42f-989859652c81"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/v1/products"
},
"profile": {
"href": "http://localhost:8081/api/v1/profile/products"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
主要问题是这种行为背后的原因是什么。我无法采用和使用它,因为我无法理解它的性质以及它是否可以解决。
解决方案
推荐阅读
- android - 模拟器:模拟器:错误:检测到挂起的线程“QEMU2 CPU0 线程”。15000 毫秒无响应
- r - 根据模式匹配创建指标变量
- c# - asp.net core 2.1, C# app, c# web service, 访问SQL数据库
- javascript - 使用 Yup 验证信用卡详细信息
- php - 在 wordpress 中显示有关特定错误消息的管理员通知
- javascript - 在 Laravel 中使用 javascript 创建控件
- javascript - 无法为 D3 分位数函数输出图例
- webpack-style-loader - Vue-cli import relative scss 不适用于供应商**外部**库**在样式文件中**
- angular - 在嵌套的 Angular formGroups 中动态分配 formControlName
- javascript - 在 Firefox 中,为什么当 javascript 更改背景颜色时 HTML 按钮的大小会减小?