Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating incomplete JSON error when reading JPA model with one to many relationship

I am using JPA EclipseLink to model a one to many relationship between UseCase and PainPoint. I am able to insert the values fine. Thereafter, I am using JAX-RS to retrieve the data using a GET method. The GET method fails with the error - Generating incomplete JSON.

Console Log:

  • [EL Fine]: INSERT INTO USECASE (UseCaseID, Description) VALUES (?, ?) bind => [1, Description]
  • [EL Fine]: INSERT INTO PAIN_POINT (PainPointID, PainPointDescription, USECASE_ID) VALUES (?, ?, ?) bind => [2, Pain Point 1, 1]
  • [EL Fine]: SELECT UseCaseID, Description FROM USECASE
  • Nov 17, 2017 7:16:22 PM org.eclipse.yasson.internal.Marshaller marshall SEVERE: Generating incomplete JSON

UseCase:

@NamedQueries({@NamedQuery(name = "getAllUseCases", query = "SELECT c FROM UseCase c")})

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@NotNull
@Column(name = "UseCaseID")
private int UseCaseID;

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

@OneToMany(mappedBy="usecase", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private Collection<PainPoint> painPoints = new ArrayList<PainPoint>();

PainPoint:

@NamedQueries({@NamedQuery(name = "getAllPainPoints", query = "SELECT c FROM PainPoint c")})

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@NotNull
@Column(name = "PainPointID")
private int PainPointID;

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

@ManyToOne
@JoinColumn (name="USECASE_ID", referencedColumnName="UseCaseID")
private UseCase usecase;

DataLoader:

UseCase useCase = new UseCase("Description 1");
PainPoint painPoint1 = new PainPoint("Pain Point 1", useCase);
useCase.getPainPoints().add(painPoint1);
em.persist(useCase);

UseCaseService:

@GET
@Path("/")
public List<UseCase> getUseCases() {

List<UseCase> retVal = null;

EntityManagerFactory emf = Utility.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();

retVal = em.createNamedQuery("getAllUseCases").getResultList();

return retVal;
}
like image 404
Milton Chandradas Avatar asked Dec 10 '22 09:12

Milton Chandradas


2 Answers

Add the annotation @JsonbTransient (package: javax.json.bind.annotation.JsonbTransient) on the getter methods for cyclic references, or any other derived fields. This is equivalent to the @XmlTranisent annotation for XML generation and will prevent the conversion to JSON entering an infinite loop.

Depends on your toolset I guess, but for me using NetBeans, when the entity class is generated automatically the annotation for XML is added but not for JSON. Just match each @XmlTransient with an equivalent @JsonbTransient

like image 62
Dale Avatar answered Jan 15 '23 21:01

Dale


According to what Dale said above. You can add the following Maven dependency

<dependency>
    <groupId>javax.json.bind</groupId>
    <artifactId>javax.json.bind-api</artifactId>
    <version>1.0</version>
</dependency>

More information about @JsonbTransient can be found in the link

like image 38
Sheila Mbadi Avatar answered Jan 15 '23 19:01

Sheila Mbadi