我遇到了JPA的问题,我收到了很多我不需要的额外数据以及很多请求。我知道我可以通过在请求中返回DTO来解决此问题,但是随后我路由了JPA的有用性,因此我想知道是否有更好的方法来解决此问题。
例如,我有两个班。一种称为产品项目,另一种称为供应商。一个产品项目,可以具有一个ManyToOne
关系的供应商。当我执行获取商品项目的获取请求时,不需要供应商的地址。我只需要名称和ID。但是,当我向供应商提出获取请求时,我确实需要与供应商有关的所有信息。除了返回没有该信息的DTO之外,还有什么方法可以指定此方法?
ProductItem.java
@Entity
public class ProductItem {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Organization org;
@ManyToOne
private Supplier supplier;
private String barCode;
private String description;
private String name;
private Integer stock;
private Integer caseQty;
private BigDecimal caseCost;
@ManyToMany(cascade=CascadeType.ALL)
private List<Note> notes;
@CreationTimestamp
private LocalDateTime createdOn;
@UpdateTimestamp
private LocalDateTime updatedOn;
Supplier.java
@Entity
public class Supplier {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(cascade=CascadeType.ALL)
private List<Note> notes;
@ManyToOne(cascade=CascadeType.ALL)
private Address address;
@ManyToOne
private Organization org;
当前获取http://localhost:8080/productitem/1
{
"id": 1,
"org": {
"id": 1,
"name": "Test",
"address": {
"name": "Test"
},
"website": null,
"phone": null
},
"supplier": {
"id": 1,
"name": "Sams Club",
"notes": [],
"address": {
"name": "Test"
},
"org": {
"id": 1,
"name": "Test",
"address": {
"name": "Test"
},
"website": null,
"phone": null
}
},
"barCode": "UPC134",
"description": "This is description. This is description. This is description. This is description. ",
"name": "Sunchips",
"stock": 10,
"caseQty": 34,
"caseCost": 29.99,
"notes": [
{
"id": 1,
"title": "Product Item Created",
"description": "Note created by user X on 12/16/2019 11:00PM"
},
{
"id": 2,
"title": "Product Item Updated",
"description": "Product ITem updated stock by user X on 12/16/2019 11:00PM"
}
],
"createdOn": "2019-02-28T13:44:39",
"updatedOn": "2019-02-28T13:44:39"
}
尝试获得http://localhost:8080/productitem/1
{
"id": 1,
"org": {
"id": 1,
"name": "Test",
"address": null,
"website": null,
"phone": null
},
"supplier": {
"id": 1,
"name": "Sams Club",
},
"barCode": "UPC134",
"description": "This is description. This is description. This is description. This is description. ",
"name": "Sunchips",
"stock": 10,
"caseQty": 34,
"caseCost": 29.99,
"notes": [
{
"id": 1,
"title": "Product Item Created",
"description": "Note created by user X on 12/16/2019 11:00PM"
},
{
"id": 2,
"title": "Product Item Updated",
"description": "Product ITem updated stock by user X on 12/16/2019 11:00PM"
}
],
"createdOn": "2019-02-28T13:44:39",
"updatedOn": "2019-02-28T13:44:39"
}
尝试实现/当前获得http://localhost:8080/supplier/1
{
"id": 1,
"name": "Sams Club",
"notes": [],
"address": {
"name": "Test"
},
"org": {
"id": 1,
"name": "Test",
"address": null,
"website": null,
"phone": null
}
答案 0 :(得分:1)
您可以使用Jackson Json Views来控制某些控制器方法的序列化/反序列化。简短示例:
查看定义:
FileName
供应商:
public class Views {
public static class BriefSupplier {
}
public static class FullSupplier extends BriefSupplier {
}
}
控制器:
@Entity
public class Supplier {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonView(Views.BriefSupplier.class)
private Long id;
@JsonView(Views.BriefSupplier.class)
private String name;
@ManyToMany(cascade=CascadeType.ALL)
@JsonView(Views.FullSupplier.class)
private List<Note> notes;
@ManyToOne(cascade=CascadeType.ALL)
@JsonView(Views.FullSupplier.class)
private Address address;
@ManyToOne
@JsonView(Views.FullSupplier.class)
private Organization org;
}
答案 1 :(得分:0)
除非您用@Transient
专门标记,否则JPA将使用该类的所有属性:
@Transient
private String agencyName;
@Column
注释纯粹是可选的,在那里您可以覆盖自动生成的列名。此外,@Column
的length属性仅在自动生成表定义时使用,对运行时间没有影响。
请确保您导入的是javax.persistence.transient
,而不是其他软件包。