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:
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;
}
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With