0

I am trying to serialize hibernate objects using gson. When I do, I get something like this:

User Object with Embedded Address Object

{ "userName" : "melchoir",
   "address" : {}
}

That's cool and all, but I want something simpler. I want a more accurate representation of the database object. Specifically, I want to see the foreign key for the address rather than the actual address object. I want something like this:

User Object with Foreign Key

{"userName" : "melchoir",
       "addressId" : 3
}

Does anyone know how I can go about getting the second example out of gson and hibernate? I should point out that the hibernate objects have already been loaded. I cannot easily make another hibernate session with different configuration options just to satisfy this. I need a solution which rests with gson.

melchoir55
  • 6,842
  • 7
  • 60
  • 106
  • 1
    Try to get only foreign key values of Address Object in User Object in first place.. Anyway show your code how you are getting the data of user object. – Rookie007 Dec 14 '15 at 08:12
  • @Babel That's a great idea. Unfortunately that is itself something I'm having trouble with. I have created a related stack overflow post here which investigates including the foreign keys on the objects when they are originally generated: http://stackoverflow.com/questions/34301634/eclipse-hibernate-pojo-generation-include-foreign-keys/34302239#34302239 – melchoir55 Dec 16 '15 at 18:26

0 Answers0