I'm trying to transform this class below using the Gson.ToJson(Object)
method, but it is returing me the object hash code of the class, eg: br.com.helpradar.entity.User@475fdaaa
However, I can retrieve the user.expertise without any problems and with all the relationships: Gson.ToJson(user.getExpertise)
@Entity
@SequenceGenerator(name="seqUser", sequenceName="SEQ_USER", allocationSize=1)
public class User {
@Id
private Long id;
@Column(nullable=false)
private String name;
@OneToOne
private Contact contact;
//enum
private TypeUser typeUser;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "USER_REVIEW",
joinColumns = { @JoinColumn(name = "USER_ID") },
inverseJoinColumns = { @JoinColumn(name = "REVIEW_ID") })
@Column(name="REVIEW")
private Set<Review> review= new HashSet<Review>();
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "USER_EXPERTISE",
joinColumns = { @JoinColumn(name = "USER_ID") },
inverseJoinColumns = { @JoinColumn(name = "EXPERTISE_ID") })
@Column(name="EXPERTISE")
private Set<Expertise> expertise = new HashSet<Expertise>();
}
This is my Gson method:
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class, new MyTypeAdapter<Expertise>())
.registerTypeAdapter(User.class, new MyTypeAdapter<Review>())
.create();
return gson.toJson(user);
This is my MyTypeAdapter:
class MyTypeAdapter<T> extends TypeAdapter<T> {
public T read(JsonReader reader) throws IOException {
return null;
}
public void write(JsonWriter writer, T obj) throws IOException {
if (obj == null) {
writer.nullValue();
return;
}
writer.value(obj.toString());
}
}
So, how do I get the Gson.ToJson(user)
to actually return a Json string so that I can use Gson.FromJson on my other end?
Thanks in advance.