Skip to Content
0
Jul 16, 2009 at 08:16 AM

JPA:A cycle is detected in the object graph

103 Views

HI Friends,

I am facing bit too many errors while working with JPA.unfortunately we dont have proper examples which can provided clarity.

Here in my case : I have Two Entities

One to One bidirectional.

EMPLOYEE

CUBICLE.

Employee.java
@Entity
@Table(name = "CS1_EMPLOYEE")
public class Employee {
	@Id
	private int id;
	private String name;
	
    @OneToOne(cascade=CascadeType.ALL)
    private Cubicle cubicle;
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Cubicle getCubicle() {
		return cubicle;
	}
	public void setCubicle(Cubicle assignedCubicle) {
		this.cubicle = assignedCubicle;
	}
}

Cubicle.java
@Entity
@Table(name = "CS1_CUBICLE")
public class Cubicle {
	@Id
	private int id;
	private String cubeName;
	// Bidirectional
	@OneToOne(mappedBy="cubicle",cascade=CascadeType.ALL)
	private Employee employee;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCubeName() {
		return cubeName;
	}
	public void setCubeName(String cubeName) {
		this.cubeName = cubeName;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee residentEmployee) {
		this.employee = residentEmployee;
	}
}

*sql file*
CREATE TABLE "CS1_EMPLOYEE"
(
    "ID"           Integer    NOT NULL,
    "NAME"         Varchar (12) UNICODE,
    "CUBICLE_ID"   Integer,
    PRIMARY KEY ("ID")
);

CREATE TABLE "CS1_CUBICLE"
(
"ID"          Integer    NOT NULL,
    "CUBENAME"      Varchar (12) UNICODE,
    PRIMARY KEY ("ID")
);

Session Bean(EJB)
when i test this method i am gettig the error(Method In Bean)

public Cubicle findCubicleById(int id)
	{
	String jpql = "select c from Cubicle c where c.id = " +id;
    	Query q = em.createQuery(jpql);
    	Cubicle c = (Cubicle)q.getSingleResult();
    	return c;
	}

error

[com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: com.sap.com.jpacasestudy1.model.Employee@1ae5bf7 -> com.sap.com.jpacasestudy1.model.Cubicle@75e38d -> com.sap.com.jpacasestudy1.model.Employee@1ae5bf7])->com.sap.engine.services.webservices.espbase.server.additions.exceptions.ProcessException: Connection IO Exception. Check nested exception for details. (Exception; nested exception is: javax.xml.bind.MarshalException

I am unable to trace what the error is.Any help from SDN Experts?

Regards

Chandra Dasari

Edited by: chandra shekar dasari on Jul 16, 2009 1:48 PM:ver1te

Edited by: chandra shekar dasari on Jul 16, 2009 1:49 PM:code re