Spring 打印JSON响应中包含类别的子类别列表
我有一张这样的桌子Spring 打印JSON响应中包含类别的子类别列表,spring,web-services,spring-boot,Spring,Web Services,Spring Boot,我有一张这样的桌子 @Where(clause = "active =1") @Entity @Table(name = "category" ) public class Category implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) private
@Where(clause = "active =1")
@Entity
@Table(name = "category" )
public class Category implements java.io.Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String name;
@Column(nullable=false,columnDefinition="int default 1")
private Integer active;
private String pic;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
private List<Product> products = new ArrayList<Product>();
@ManyToOne
@JoinColumn(name="parent_id")
@JsonIgnore
// @ColumnDefault("0")
private Category parentId;
@OneToMany(mappedBy="parentId")
private List<Category> subCategories=new ArrayList<>();
setters and getters
}
这是此表的实体类
Where(clause = "active =1")
@Entity
@Table(name = "category", catalog = "businessin")
public class Category implements java.io.Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String name;
private Integer parentId;
private Integer active;
private String pic;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
private List<Product> products = new ArrayList<Product>();
setters&getter
}
对此
[
{
id: 1,
name: "Electronics",
parentId: 0,
active: 1,
pic: null
subCategories: [Mobile, Laptops]
}]
让你的实体像这样很简单
@Where(clause = "active =1")
@Entity
@Table(name = "category" )
public class Category implements java.io.Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
private String name;
@Column(nullable=false,columnDefinition="int default 1")
private Integer active;
private String pic;
@JsonIgnore
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
private List<Product> products = new ArrayList<Product>();
@ManyToOne
@JoinColumn(name="parent_id")
@JsonIgnore
// @ColumnDefault("0")
private Category parentId;
@OneToMany(mappedBy="parentId")
private List<Category> subCategories=new ArrayList<>();
setters and getters
}
@Where(子句=“active=1”)
@实体
@表(name=“category”)
公共类类别实现java.io.Serializable{
@身份证
@生成值(策略=标识)
@列(name=“id”,unique=true,nullable=false)
私有整数id;
私有字符串名称;
@列(nullable=false,columnDefinition=“int default 1”)
私有整数活动;
私有字符串pic;
@杰索尼奥雷
@OneToMany(fetch=FetchType.LAZY,mappedBy=“category”)
私有列表产品=新的ArrayList();
@许多酮
@JoinColumn(name=“parent\u id”)
@杰索尼奥雷
//@ColumnDefault(“0”)
私有类parentId;
@OneToMany(mappedBy=“parentId”)
私有列表子类别=新建ArrayList();
二传手和接球手
}
我认为您需要将“整数父ID”更改为“类别父ID”。然后,您需要建立一个关系(可能是OneToMany)来列出由父类映射的子类别。建议不错,但不起作用,非法尝试取消对基本类型的路径源[null.parent]的引用,并且在repository@dashaunah中出现异常,因此,也许可以将类别父类更改回整数,并改为“mapped by”。null.parent是因为Electronics没有父项。仅当spring在类别父项上创建数据库so@ManyToOne时,就会出现此错误。(我意识到我需要提高我在这里的技能,我只是想帮你)