0

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.

Community
  • 1
  • 1
okcomputer_kid
  • 491
  • 6
  • 12
  • Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Alan Hay Oct 18 '16 at 19:58
  • @AlanHay Thanks for the link. – okcomputer_kid Oct 18 '16 at 20:04
  • OK but do not rely on toString () for app logic. Use a library such as Jackson to easily convert object to Json and vice versa. http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/ – Alan Hay Oct 18 '16 at 20:06
  • I am using GSON for that purpose. GSON builds proper key-value JSON arrays for other lists which are manually built but only in this case( mapped list) , it doesnot have key-value pair, but it has list of these.."[com.zednx.tech.persistence.Click@29df9a77]" – okcomputer_kid Oct 18 '16 at 20:13
  • The conversion from a `Click` object to "com.zednx.tech.persistence.Click@29df9a77" is being done by your `MyTypeAdapter` class. Please show the code of that class. Also, what happens if you remove the registration of a custom type adapter? – Douglas Oct 18 '16 at 22:37

2 Answers2

0

You need to implement a toString method, as your current Click class likely doesn't have one, so it just prints as the name of the class and instance identifier.

Ben Harris
  • 1,734
  • 3
  • 15
  • 24
  • Thanks for the reply. toString method will only allow me change the format of object identifier right? I am using GSON for json building. I returns correct JSON array(Key Value Pairs) for other lists but returns "[com.zednx.tech.persistence.Click@29df9a77]" for the mapped List. – okcomputer_kid Oct 18 '16 at 20:10
  • toString is just changing the identifier format but there is no effect in JSON building. – okcomputer_kid Oct 18 '16 at 20:10
  • It changes whatever is output - you could use a JSON builder in your toString method to convert the object to a JSON String when toString is called. – Ben Harris Oct 18 '16 at 20:13
  • Without seeing more code (other lists?) (Click class itself?) can't really comment on why it's working in one scenario but not another. – Ben Harris Oct 18 '16 at 20:15
  • Ok now I am confused. How can I build a json in toString method. Should I just use GSON builder in toString itself?? – okcomputer_kid Oct 18 '16 at 20:16
  • You could do that, yes. – Ben Harris Oct 18 '16 at 20:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126048/discussion-between-zuqrix-and-ben-harris). – okcomputer_kid Oct 18 '16 at 20:21
0

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.

okcomputer_kid
  • 491
  • 6
  • 12