0

I have the following POJO's:

public class Prices implements Serializable{
    private static final long serialVersionUID = 1L;
    @Expose private Integer idPrice;
    @Expose private String date;
    @Expose private Double price;
    @Expose private OrderLine orderLine;

    public Prices(){}
    // getters and setters
}

public class OrderLine implements Serializable {
    private static final long serialVersionUID = 1L;
    @Expose private Integer idOrderLine;
    @Expose private Articles art;
    @Expose private Integer q;

    private List<Prices> prices;

    public OrderLine(){}
    // getters and setters  
}

I'm trying to parse a price object to JSON by using the following method:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String response = gson.toJson(price);

... but somehow GSON doesn't parse the OrderLine contained in the Price object.
If i do:

 System.out.println("Price: " + response);

I get the following answer:

Price: {"idPrice":1,"date":"2014-08-24","price":37.0,"orderLine":{}}

I'm using Hibernate too but I don't think it is the problem because if I do:

System.out.println(price);

... then I get the whole object data.

What am I doing wrong ?

Update:

This is how i initialize my price variable. (must say i'm new to Hibernate and Gson)

public static String getPrice(Integer idPrice) {

  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction tx = session.beginTransaction();

  String hql = "from Prices p where p.idPrice = :idPrice";

  Prices price = (Prices) session.createQuery(hql)
                  .setParameter("idPrice", idPrice)
                  .uniqueResult();

  Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
  String response = gson.toJson(price);


  // i have added the following code just to see what results i was getting
  System.out.println(price); <--- this shows all the object's data
  System.out.println(response); <-- this shows Price: {"idPrice":1,"date":"2014-08-   24","price":37.0,"orderLine":{}}


  tx.commit();
  session.flush();
  session.close();

  return response;
}
Martin
  • 215
  • 5
  • 14
  • Could you show us how you initialise your `price` variable? Are you sure that you're correctly setting its `orderLine`? – ccjmne Aug 24 '14 at 22:46
  • i've added an update – Martin Aug 24 '14 at 23:53
  • By `this shows all the objects's data`, you mean something like `{"idPrice":1,"date":"2014-08-24","price":37.0,"orderLine":{"idOrderLine":123,"q":456}}`? I believe that your `price` object actually isn't properly populated. Maybe your query need to `join` on the OrderList table or something. I'd suggest you to use a debugger to see whether your `price` object actually has its `orderLine` set as you expect. – ccjmne Aug 25 '14 at 00:04
  • thanks, now i know where to look. as soon as i find the solution i will post it. – Martin Aug 25 '14 at 00:50

0 Answers0