【问题标题】:(Do not display relationship values)(不显示关系值)
【发布时间】: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的输出

enter image description here

【问题讨论】:

  • 你能告诉我你的ControllerService吗?
  • 这里你不能定义你的问题是什么
  • @FaeemazazBhanej 我还没有创建它们,我使用弹簧数据休息
  • @امیرضااندیشمند 请重新编辑您的问题以改进格式。在当前状态下,无法阅读。谢谢!另外,请提供您实际使用@JsonIdentityInfo时生成的JSON。您当前显示的结果是使用 @JsonManagedReference @JsonBackReference 的结果,正如您最初的问题所描述的那样(这是在任何情况下都不要做的另一件事,更改问题的范围,否则您可能会使先前的答案)。谢谢!
  • @JoãoDias 好的,谢谢你-我重新编辑了问题并更新了输出

标签: spring-boot spring-data-jpa spring-data-rest


【解决方案1】:

这正是因为您使用了@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&lt;Article&gt; 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<>();
}

如果这些都不适合您的需求,您可能需要实现自己的自定义序列化程序。您可以在https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion 阅读有关所有这些选项的更多信息。

【讨论】:

  • 感谢您的信息。我尝试了这些方法,但无济于事
  • 这是什么意思?当您应用这些建议时,结果如何?至少@JsonIgnore 会按照您的意图工作。在尝试任何建议的方法时,您是否删除了 @JsonManagedReference@JsonBackReference
  • 我在 Article 类中使用 JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 并删除 JsonManagedRefrence 和 jsonBackRefrence 但结果没有改变,我使用 JsonIgnoe up private Set
    文章,但这并没有改变
  • 您能否将完整的ArticleArticleCategory 添加到您的答案中?另外,能否请您添加您的 Spring Data Rest 接口?谢谢!
  • 是 当然 *****************************
【解决方案2】:

我使用控制器解决了问题 这就是 @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();
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多