Loop in object graph discovered in JPA

I have been trying to figure out this error since 5 o'clock without any success. I finally thought about posting here. Please help, I am really in big trouble. I am stuck on this and see no way to solve this error. This is my database structure

tblBlogRegion                                   

BlogRegionId (primary key)              
BlogRegionName                                  


tblGadget

GadgetId(primary key)
GadgetName

tblBlogs

BlogId(primary key)                     
Blogname
BlogTypeId (reference key from tblSiteTerm


tblSiteTerms        

SiteTermsId(primary key)        
SiteTermsName   


tblBlogGadgets

BlogGadgetsId(primary key)
BlogRegionId(foreign key from tblBlogRegion)
BlogId(foreign key from tblBlog)
GadgetId(foreign key from tblGadget)

      

Isn't this a normal database structure? Do you see anything cyclical? When I try to get a list from tblGadgets I get this error: -

[com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML: entity.BlogGadgets[blogGadgetsId=1] -> entity.Blogs[blogId=2] -> entity.BlogGadgets[blogGadgetsId=1]]

      

I am trying to get a list from a web service using JAS-WS.

+2


a source to share


2 answers


I am assuming that you are using an ORM to fetch data from the database and then serialize it and send it through a web service. So when you select a list BlogGadgets

, it has Blogs

, but the same Blogs

has a list of the same, BlogGadgets

and so on ...

Thats called circular reference and it's very common to have them on your object model, but when you want to wire it up, you need to make sure you have an object graph, at least for the serializer you are using.

One solution to this would be to convert / map the domain objects / objects you are using to data transfer objects which will have a proper tree structure like: (in C #, but it should be the same in java)



[Serializable]
public class BlogGadgetDto 
{
  public int GadgetId {get;set;}
  public BlogSmallDto Blog {get;set;}
  // and so on
}

[Serializable]
public class BlogSmallDto 
{
  public int BlogId {get;set;}
  public string BlogName {get;set;}
  // and so on
}

      

Note that it is now BlogGadgetDto

referenced BlogSmallDto

, but not vice versa.

+4


a source


The solution just adds annotation:

import javax.xml.bind.annotation.XmlTransient;

@XmlTransient

      



on the property getter calling the loop.

-1


a source







All Articles