Question

This is a piece of class, that I've mapped into Hibernate:

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="hall_id")

Set<Literature> literatures;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="hall_id")

Set<RecycleFacility> recycleFacilities;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="hall_id")

Set<Assent> assents;

But when I'm trying to map this class to JSon, I always have a stacks. I was using Jackson Mapper, Genson, and Google Gson, but they can't map this piece of code (or I don't know how to map this). Can anyone tell me, how to map propertly this kind of sets?

Était-ce utile?

La solution

Assuming your json format is like this

 {
   "literatures":[
      {
         "id":10,
         "name":"abc"
      }
   ],
   "recycleFacilities":[
      {
         "id":20,
         "name":"testFaculty"
      }
   ],
   "assents":[
      {
         "id":30,
         "name":"Test"
      }
   ]
}

and assuming your object model classes are composed in another class as like

Class Another{ 

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="hall_id")

Set<Literature> literatures;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="hall_id")

Set<RecycleFacility> recycleFacilities;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="hall_id")

Set<Assent> assents;
// getter/setter
}

Then you can parse and and populate your hibernate object model as below.

Another response = gson.fromJson(reader, Another.class);

You can create json from model as below

Gson gson = new Gson();
String jsonString= gson.toJson(yourObject);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top