Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

List<Entity> to JSON

RobalApr 19 2017

Dear Experts,

How to convert list of entites  to Json string?

entity1:

@Entity

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

@Table(name = "DEPARTMENT")

public class Departament implements Serializable {

    @Id

    @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:

@Entity

@Table(name = "ACCESS_RIGHTS")

public class AccessRights implements Serializable {

    @Id

    @Column(nullable = false)

    private Long id;

    @ManyToOne

    @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.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2017
Added on Apr 19 2017
0 comments
471 views