【发布时间】:2022-01-10 09:33:21
【问题描述】:
我有两个实体,分别是文章名称和文章类别。 他们有一对多的关系。 我使用@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property = "id") 但是我在spring data rest中看不到文章类别(category_id)的数据。
ArticleCategory.class
@Entity
@Table(name = "article_category")
@Getter
@Setter
public class ArticleCategory implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "category_name")
private String categoryName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "articleCategory", fetch = FetchType.LAZY)
private Set<Article> articles = new HashSet<>();
}
文章.class
@Entity
@Table(name = "article")
@Getter
@Setter
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Article implements Serializable {
public Article() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private ArticleCategory articleCategory;
@Column(name = "title")
private String title;
@Column(name = "image_url")
private String image_url;
@Column(name = "short_description")
private String short_description;
@Column(name = "text")
private String text;
@Column(name = "keywords", nullable = true)
private String keywords;
@Column(name = "visit", nullable = false)
private int visit;
@Column(name = "code", nullable = false)
private UUID code;
@Column(name = "date_created")
@CreationTimestamp
private Date dateCreated;
@Column(name = "date_updated", nullable = false)
@UpdateTimestamp
private Date dateUpdated;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
public Article(String title, String image_url, String short_description, String text, String keywords, int visit, UUID code) {
this.title = title;
this.image_url = image_url;
this.short_description = short_description;
this.text = text;
this.keywords = keywords;
this.visit = visit;
this.code = code;
}
}
文章存储库
@CrossOrigin("http://localhost:4200")
@RepositoryRestResource(collectionResourceRel = "article", path = "article")
public interface ArticleRepository extends JpaRepository<Article,Long> {
Article findByCode(UUID uuid);
}
这是spring data rest的输出
【问题讨论】:
-
你能告诉我你的
Controller和Service吗? -
这里你不能定义你的问题是什么
-
@FaeemazazBhanej 我还没有创建它们,我使用弹簧数据休息
-
@امیرضااندیشمند 请重新编辑您的问题以改进格式。在当前状态下,无法阅读。谢谢!另外,请提供您实际使用
@JsonIdentityInfo时生成的JSON。您当前显示的结果是使用@JsonManagedReference和@JsonBackReference的结果,正如您最初的问题所描述的那样(这是在任何情况下都不要做的另一件事,更改问题的范围,否则您可能会使先前的答案)。谢谢! -
@JoãoDias 好的,谢谢你-我重新编辑了问题并更新了输出
标签: spring-boot spring-data-jpa spring-data-rest