<i id='8mW1g'><tr id='8mW1g'><dt id='8mW1g'><q id='8mW1g'><span id='8mW1g'><b id='8mW1g'><form id='8mW1g'><ins id='8mW1g'></ins><ul id='8mW1g'></ul><sub id='8mW1g'></sub></form><legend id='8mW1g'></legend><bdo id='8mW1g'><pre id='8mW1g'><center id='8mW1g'></center></pre></bdo></b><th id='8mW1g'></th></span></q></dt></tr></i><div id='8mW1g'><tfoot id='8mW1g'></tfoot><dl id='8mW1g'><fieldset id='8mW1g'></fieldset></dl></div>

<small id='8mW1g'></small><noframes id='8mW1g'>

    1. <tfoot id='8mW1g'></tfoot>

        • <bdo id='8mW1g'></bdo><ul id='8mW1g'></ul>
      1. <legend id='8mW1g'><style id='8mW1g'><dir id='8mW1g'><q id='8mW1g'></q></dir></style></legend>

        所有嵌入式注释对象的单个自定义序列化程序,用它们的 id 替换它们

        时间:2024-08-24
      2. <tfoot id='Jskd5'></tfoot>
        <i id='Jskd5'><tr id='Jskd5'><dt id='Jskd5'><q id='Jskd5'><span id='Jskd5'><b id='Jskd5'><form id='Jskd5'><ins id='Jskd5'></ins><ul id='Jskd5'></ul><sub id='Jskd5'></sub></form><legend id='Jskd5'></legend><bdo id='Jskd5'><pre id='Jskd5'><center id='Jskd5'></center></pre></bdo></b><th id='Jskd5'></th></span></q></dt></tr></i><div id='Jskd5'><tfoot id='Jskd5'></tfoot><dl id='Jskd5'><fieldset id='Jskd5'></fieldset></dl></div>

              <bdo id='Jskd5'></bdo><ul id='Jskd5'></ul>

                  <small id='Jskd5'></small><noframes id='Jskd5'>

                    <tbody id='Jskd5'></tbody>

                  <legend id='Jskd5'><style id='Jskd5'><dir id='Jskd5'><q id='Jskd5'></q></dir></style></legend>
                • 本文介绍了所有嵌入式注释对象的单个自定义序列化程序,用它们的 id 替换它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这样的实体:

                  @Entity
                  public Product {
                     @Id
                     public int id;
                  
                     public String name;
                  
                     @ManyToOne(cascade = {CascadeType.DETACH} )
                     Category category
                  
                     @ManyToMany(cascade = {CascadeType.DETACH} )
                     Set<Category> secondaryCategories;
                  
                  
                  }
                  

                  @Entity
                  public Category {
                     @Id
                     public int id;
                  
                     public String name;
                  
                      @JsonCreator
                      public Category(int id) {
                          this.id = id;
                      }
                  
                      public Category() {}
                  }
                  

                  是否可以仅注释 Category 类或 categorysecondaryCategories 属性,并使用注释将它们序列化为它们的 id当它们被嵌入时.

                  is it possible to annotate either just Category class or category and secondaryCategories properties with an annotation that will serialize them to be just their ids when they are embedded.

                  现在,当我为 id=1 的产品创建 GET 时,我正在从服务器获取信息:

                  right now I am getting from the server when I make a GET for product with id=1:

                  { 
                    id: 1,
                    name: "product 1", 
                    category: {id: 2, name: "category 2" }, 
                    secondaryCategories: [{id: 3, name: "category 3" }, 
                                          {id: 4, name: "category 4" }, 
                                          {id: 5, name: "category 5" }] 
                  }
                  

                  有没有可能回来:

                  { 
                    id: 1,
                    name: "product 1", 
                    category: 2, 
                    secondaryCategories: [3, 4, 5]
                  }
                  

                  使用 @JsonIdentityReference(alwaysAsId = true) 注释 Category通常工作,但当我获取一个或一个类别列表时也只返回 ids.只有在嵌入 Category 时才需要 id 转换.

                  Annotating Category class with @JsonIdentityReference(alwaysAsId = true) works generally but also returns just ids when I am fetching one or a list of Categories. I need id conversion only when Category is embedded.

                  谢谢!

                  推荐答案

                  您只需要在类别变量上使用 @JsonIdentityReference(alwaysAsId = true).

                  You need to use @JsonIdentityReference(alwaysAsId = true) on the category variable only.

                  例如:

                  @Entity
                  public Product {
                     @Id
                     public int id;
                  
                     public String name;
                  
                     @ManyToOne(cascade = {CascadeType.DETACH} )
                     @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope=Category.class)
                     @JsonIdentityReference(alwaysAsId = true)
                     Category category;
                  
                     @ManyToMany(cascade = {CascadeType.DETACH} )
                     Set<Category> secondaryCategories;
                  
                  
                  }
                  

                  这篇关于所有嵌入式注释对象的单个自定义序列化程序,用它们的 id 替换它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Spring Data REST 不包括资源中的实体链接 下一篇:休眠查询以过滤嵌套对象列表中的结果

                  相关文章

                  1. <legend id='ROVA1'><style id='ROVA1'><dir id='ROVA1'><q id='ROVA1'></q></dir></style></legend>

                    <small id='ROVA1'></small><noframes id='ROVA1'>

                  2. <tfoot id='ROVA1'></tfoot>
                      <bdo id='ROVA1'></bdo><ul id='ROVA1'></ul>

                    <i id='ROVA1'><tr id='ROVA1'><dt id='ROVA1'><q id='ROVA1'><span id='ROVA1'><b id='ROVA1'><form id='ROVA1'><ins id='ROVA1'></ins><ul id='ROVA1'></ul><sub id='ROVA1'></sub></form><legend id='ROVA1'></legend><bdo id='ROVA1'><pre id='ROVA1'><center id='ROVA1'></center></pre></bdo></b><th id='ROVA1'></th></span></q></dt></tr></i><div id='ROVA1'><tfoot id='ROVA1'></tfoot><dl id='ROVA1'><fieldset id='ROVA1'></fieldset></dl></div>