2

I have two entities: Category and Item.

Category Entity:

@JsonInclude(Include.NON_EMPTY)
@JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class)
@Entity
public class Category {
    @Id
    @GeneratedValue
    private int id;

    private String categoryName;    

    @OneToMany(mappedBy = "category")   
    private List<Item> itemList;

    //have getters and setters  
}

Item Entity:

    @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class)
    @Entity
    public class Item {

    @Id
    @GeneratedValue
    private int id;
    private String itemName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_Category_id")    
    private Category category;

    //have getters and setters              
}

After doing a left join query like this: "SELECT c from Category c Left JOIN FETCH c.itemList il"

I got the result and then I have done hibernate aware JSON serialization to the result.

My HibernateAwareObjectMapper is:

public class HibernateAwareObjectMapper extends ObjectMapper {
          public HibernateAwareObjectMapper() {   
                Hibernate4Module hibernateModule = new Hibernate4Module();
                hibernateModule.disable(Hibernate4Module.Feature.FORCE_LAZY_LOADING);
                registerModule(hibernateModule);
          }
        } 

And HibernateAwareSerializerFactory is:

public class HibernateAwareSerializerFactory extends BeanSerializerFactory {
                protected HibernateAwareSerializerFactory(SerializerFactoryConfig config) {
                    super(config);
                }
            }

In my dispatcher servlet I have written:

    <mvc:annotation-driven>
                <mvc:message-converters>
                    <bean
                        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="objectMapper">
                            <bean class="org.scinv.hibernateAwareMapper.HibernateAwareObjectMapper" />
                        </property>
                    </bean>
                </mvc:message-converters>
        </mvc:annotation-driven>

So, I have got a JSON Like this:

   {
      "ArrayList": [
        {
          "@id": "971ef69e-1605-46f2-8234-b595e38be11a",
          "id": 1,
          "categoryName": "fruit",
          "itemList": [
            {
              "@id": "75a3a7e5-ce66-4f6d-a04c-d04145d92b21",
              "id": 1,
              "itemName": "mango",
              "category": "971ef69e-1605-46f2-8234-b595e38be11a"
            },
            {
              "@id": "0aa0fb71-2909-4909-8403-0765829ee8c1",
              "id": 2,
              "itemName": "apple",
              "category": "971ef69e-1605-46f2-8234-b595e38be11a"
            },
            {
              "@id": "02c381cb-33fa-45a6-bff9-ec146357f4bc",
              "id": 3,
              "itemName": "orange",
              "category": "971ef69e-1605-46f2-8234-b595e38be11a"
            }
          ]
        },
        "971ef69e-1605-46f2-8234-b595e38be11a",
        "971ef69e-1605-46f2-8234-b595e38be11a"    
      ]
    }

In this JSON, there is "@id": "971ef69e-1605-46f2-8234-b595e38be11a" which is Object Id for Category Entity.

And there is "@id": "75a3a7e5-ce66-4f6d-a04c-d04145d92b21" which is Object Id for Item Entity.

Category has 3 items So Catagory's object Id is also showing 2 more times. Like this: "971ef69e-1605-46f2-8234-b595e38be11a","971ef69e-1605-46f2-8234-b595e38be11a"

That Generated JSON serialization is as expected. But I just need to hide that Object reference "@id" in that json. This "@id" is by default created by @JsonIdentityInfo.

So I need configuration for @JsonIdentityInfo to hide Object Id "@id". I have tried @JsonIgnore but failed.

I have googled 2 days for solution but failed.Can anyone give me a solution or a suggestion?

Shaiful
  • 928
  • 1
  • 10
  • 11
  • How did you do the "hibernate aware JSON serialization"? I think the problems is there – KSTN Sep 16 '15 at 08:23
  • @MangEngkus I have added HibernateAwareSerializerFactory, HibernateAwareObjectMapper and dispatcher servlet. But I think they are ok.Because it is generating JSON serialization as expected.But I just need to hide Object reference //@id// in that json. Which is by default created by //@JsonIdentityInfo//. So I need configuration for //@JsonIdentityInfo// to hide Object reference "//@id//" – Shaiful Sep 16 '15 at 11:45
  • why you use the annotation if you don't want it to generate the Object reference?if you don't have control to modify the source code then you can disable the annotation on `HibernateAwareObjectMapper` – KSTN Sep 16 '15 at 14:36
  • I have used @JsonIdentityInfo annotation to solve infinite recursion problem in HibernateAware JSON serilization. – Shaiful Sep 16 '15 at 18:56
  • 1
    Have you try `@JsonManageReference` and `@JsonBackReference` to avoid infinite recursion? – KSTN Sep 17 '15 at 00:43
  • 1
    Yes, I have used `@JsonManageReference` and `@JsonBackReference`. But this time I wanted to use `@JsonIdentityInfo`. This `@JsonIdentityInfo` automatically generate **Object Id** like this: `"@id": "75a3a7e5-ce66-4f6d-a04c-d04145d92b21"` I just want to hide this **object id** in JSON. – Shaiful Sep 17 '15 at 08:05

0 Answers0