0

I try to convert JSON to java objects, specifically to entities for persisting with hibernate. For example, I have such JSON:

{
   items: [
      {
         id: 100,
         warehouse: {
            id: 1,
            stars: 5
         }
      },
      {
         id: 101,
         warehouse: {
            id: 1,
            stars: 5
         }
      }                
   ]
}

Is there any parser which can handle warehouse for id=100 and warehouse for id=101 as one object and as a result after parsing there will be in memory only one instance of warehouse and each item will have same reference to this warehouse.

Now I'm using for parsing Jackson library with Hibernate3Module. But according to this example it creates new warehouse instance for each item - different objects, but actually with identical state.

P.S. There is similiar post GSON + Hibernate: Identical objects causing “An entity copy was already assigned to a different entity”. According to it there is no general decision for this task. But it dates back to 2013.

pesiykot
  • 59
  • 1
  • 3
  • 9
  • After deserializing there will always be two instances because there are two json objects. You will have to narrow down your list by either finding duplicates yourself or using a ``Set``. Otherwise you need a custom deserializer. – f1sh Aug 29 '16 at 15:44
  • @f1sh, It seems to be that described case quite common, for which there can be a solution in one of the parsing lib. – pesiykot Aug 29 '16 at 16:04

0 Answers0