Zuqrix Zuqrix - 1 month ago 9
JSON Question

Hibernate One-To-Many Mapping, printing mapped lists

I have used One-to-Many Mapping in my project. I have stored a list of clicks for every user.
But when I retrieve the list by calling getClicks() methodm Hibernate returns list in different format.
Something like this.



"[com.zednx.tech.persistence.Click@29df9a77]"





So I tried Reading Every value from the list and assign to a new List.



List<Click> clicks=new ArrayList<Click>();
for(Click c: e.getClicks()){
Click temp = new Click();
temp.setAff_source(c.getAff_source());
temp.setCb_to_award(c.getCb_to_award());
temp.setCb_type(c.getCb_type());
clicks.add(temp);
}





But when i print the items of new List it stills prints the same way.

I need to build a JSON from the resulting String of this list.
So if the list is returned in format, it wont help me.

I couldn't find anything regarding this except How to pretty print Hibernate query results?

I tried Arrays.ToString(Object o). But it doesn't work.

GSON builder part-



Gson gson = new GsonBuilder()
.registerTypeAdapter(Click.class, new MyTypeAdapter<Click>())
.create();
List<Click> clicks=new ArrayList<Click>();
for(Click c: e.getClicks()){
Click temp = new Click();
temp.setAff_source(c.getAff_source());
temp.setCb_to_award(c.getCb_to_award());
temp.setCb_type(c.getCb_type());
temp.setCom_to_recieve(c.getCom_to_recieve());
temp.setStore_name(c.getStore_name());
temp.setT_date(c.getT_date());
temp.setT_status(c.getT_status());
temp.setT_ticket(c.getT_ticket());
temp.setUid(c.getUid());
System.out.println(c.toString());
clicks.add(temp);
}
String json = gson.toJson(clicks, Click.class);





Click.java



@Entity
@Table(name="click")
public class Click {

@Id
@Column(name="t_ticket")
private String t_ticket;

@Column(name="uid",nullable=false)
private long uid;

public long getUid() {
return uid;
}

public void setUid(long uid) {
this.uid = uid;
}

@ManyToOne
@JoinColumn(name="uid",
insertable=false, updatable=false,
nullable=false)
private Earning earning;

@Column(name="store_name")
private String store_name;

@Column(name="t_status")
private String t_status;

@Column(name="aff_source")
private String aff_source;

@Column(name="com_to_recieve")
private float com_to_recieve;

@Column(name="t_date")
private Date t_date;

@Column(name="cb_to_award")
private float cb_to_award;

@Column(name="cb_type")
private String cb_type;

public String getT_ticket() {
return t_ticket;
}

public void setT_ticket(String t_ticket) {
this.t_ticket = t_ticket;
}



public Earning getEarning() {
return earning;
}

public void setEarning(Earning earning) {
this.earning = earning;
}

public String getStore_name() {
return store_name;
}

public void setStore_name(String store_name) {
this.store_name = store_name;
}

public String getT_status() {
return t_status;
}

public void setT_status(String t_status) {
this.t_status = t_status;
}

public String getAff_source() {
return aff_source;
}

public void setAff_source(String aff_source) {
this.aff_source = aff_source;
}

public float getCom_to_recieve() {
return com_to_recieve;
}

public void setCom_to_recieve(float com_to_recieve) {
this.com_to_recieve = com_to_recieve;
}

public Date getT_date() {
return t_date;
}

public void setT_date(Date t_date) {
this.t_date = t_date;
}

public float getCb_to_award() {
return cb_to_award;
}

public void setCb_to_award(float cb_to_award) {
this.cb_to_award = cb_to_award;
}

public String getCb_type() {
return cb_type;
}

public void setCb_type(String cb_type) {
this.cb_type = cb_type;
}





Any Help is appreciated.

Answer

Okay, I could solve my problem finally. I made another POJO without any annotations and Mapped the List items to that POJO class. I think the problem was with Annotation of mapping on another class which I had in original POJO.

Also getString() method only helps in changing format of identifier. So basically it has nothing to do with JSON building unless you format getString() in form of JSON.

Hope it helps. If anyone wants new temp POJO I made I can post it if requested. Thanks.