(不显示关系值)
(Do not display relationship values)
我有两个实体,分别是文章名称和文章类别。
他们有一对多的关系。
我使用 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,属性 = "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<>();
}
Article.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 数据 rest
的输出
enter image description here
那正是因为您使用了 @JsonManagedReference
和 @JsonBackReference
。使用它们时请记住以下几点:
@JsonManagedReference
是关系的前向部分,是正常序列化的部分。
@JsonBackReference
是关系后面的部分,连载时会省略。
- 序列化的
Article
对象不包含对 ArticleCategory
对象的引用。
如果您想在序列化 Article
时获得任何 ArticleCategory
数据,您可以使用 @JsonIdentityInfo
以便序列化其中一个属性(在本例中我选择了 id
两者):
@Entity
@Table(name = "article")
@Getter
@Setter
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Article implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private ArticleCategory articleCategory;
}
@Entity
@Table(name = "article_category")
@Getter
@Setter
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
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<>();
}
如果您只对 categoryId
感兴趣,另一种可能性是在 private Set<Article> articles
属性 上使用 @JsonIgnore
,这样它就不会被序列化:
@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;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL,mappedBy = "articleCategory" ,fetch = FetchType.LAZY)
private Set<Article> articles=new HashSet<>();
}
如果其中 none 满足您的需求,您可能需要实施自己的自定义序列化程序。您可以在 https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion.
阅读有关所有这些选项的更多信息
我用控制器解决了问题
这就是为什么@JsonManageRefrence 和@JsonBackRefrence 不起作用
我在两个实体中都用急切加载替换了延迟加载
@ManyToOne(fetch = FetchType.Eager)
@JoinColumn(name = "user_id")
@JsonManageRefrence
private User user;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "articleCategory",
fetch = FetchType.Eager)
@JsonBackRefrence
private Set<Article> articles = new HashSet<>();
然后添加控制器
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/getAllArticle")
public class MyController {
private ArticleRepository articleRepository;
// you must do constructor injection
@GetMapping("/getAllArticle")
public List<Article> allArticle()
{
return articleRepository.findAll();
}
}
我有两个实体,分别是文章名称和文章类别。 他们有一对多的关系。 我使用 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,属性 = "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<>();
}
Article.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 数据 rest
的输出enter image description here
那正是因为您使用了 @JsonManagedReference
和 @JsonBackReference
。使用它们时请记住以下几点:
@JsonManagedReference
是关系的前向部分,是正常序列化的部分。@JsonBackReference
是关系后面的部分,连载时会省略。- 序列化的
Article
对象不包含对ArticleCategory
对象的引用。
如果您想在序列化 Article
时获得任何 ArticleCategory
数据,您可以使用 @JsonIdentityInfo
以便序列化其中一个属性(在本例中我选择了 id
两者):
@Entity
@Table(name = "article")
@Getter
@Setter
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Article implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private ArticleCategory articleCategory;
}
@Entity
@Table(name = "article_category")
@Getter
@Setter
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
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<>();
}
如果您只对 categoryId
感兴趣,另一种可能性是在 private Set<Article> articles
属性 上使用 @JsonIgnore
,这样它就不会被序列化:
@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;
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL,mappedBy = "articleCategory" ,fetch = FetchType.LAZY)
private Set<Article> articles=new HashSet<>();
}
如果其中 none 满足您的需求,您可能需要实施自己的自定义序列化程序。您可以在 https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion.
阅读有关所有这些选项的更多信息我用控制器解决了问题 这就是为什么@JsonManageRefrence 和@JsonBackRefrence 不起作用
我在两个实体中都用急切加载替换了延迟加载
@ManyToOne(fetch = FetchType.Eager)
@JoinColumn(name = "user_id")
@JsonManageRefrence
private User user;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "articleCategory",
fetch = FetchType.Eager)
@JsonBackRefrence
private Set<Article> articles = new HashSet<>();
然后添加控制器
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/getAllArticle")
public class MyController {
private ArticleRepository articleRepository;
// you must do constructor injection
@GetMapping("/getAllArticle")
public List<Article> allArticle()
{
return articleRepository.findAll();
}
}