0 Replies Latest reply on Apr 19, 2017 2:58 PM by Robal

    List<Entity> to JSON


      Dear Experts,


      How to convert list of entites  to Json string?



      @NamedQueries({ @NamedQuery(name = "Departament.findAll", query = "select o from Departament o"))

      @Table(name = "DEPARTMENT")

      public class Departament implements Serializable {


          @Column(nullable = false)

          private Long id;

          @Column(length = 119)

          private String name;


          @OneToMany(mappedBy="department", cascade = { CascadeType.PERSIST, CascadeType.MERGE })

          private List<AccessRights> perms;

      // cut: setters and getters


      entity 2:


      @Table(name = "ACCESS_RIGHTS")

      public class AccessRights implements Serializable {


          @Column(nullable = false)

          private Long id;



          @JoinColumn(name = "ID_JEDNOSTKA")

          private Departament department;

      // cut: setters and getters



      In my main class I have executed:

              List<Departament> vl = ac.getDepartament();

              System.out.println(new Gson().toJson(vl).toString()); //error: "An attempt was made to traverse a relationship using indirection that had a null Session."


      The has started to orccured when I've created ManyToOne relationship. If I remove it, everything works.

      I assume that toJson method cannot be applied. If there is any relation between entities.


      I'm I correct? How such an issue can be resolved?

      I've tried other parsers like Objectmapper, JsonLib - no success.


      Best regards.