开发者

A cycle in object graph detected in JPA

开发者 https://www.devze.com 2022-12-27 21:25 出处:网络
I am trying to figure out this error since 5 hours without any success. SO i finally thought of posting in here. Please help i am really in big trouble. I am stuck on this and see no way of solving th

I am trying to figure out this error since 5 hours without any success. SO i finally thought of posting in here. Please help i am really in big trouble. I am stuck on this and see no way of solving 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)

Is it not normal database structure? Do you see anything that is cyclic? WHen i try to fetch 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 t开发者_JAVA技巧rying to get list from web service using JAS-WS.


I suppose you are using some ORM to fetch data from database and then serialize it and send via web service. So when you fetch list of BlogGadgets they have Blogs in it, but the same Blogs have a list of the same BlogGadgets and so on...

Thats called circular reference and its very common to have them at your object model but when you want to send it on wire you need to make sure that object graph you have is a tree, at least for serializer you are using.

One solution to this would be to transform/map domain objects/entities you are using to Data Transfer Objects which will have proper tree structure like: (in c# but it should be pretty 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 now BlogGadgetDto are referencing BlogSmallDto but not the other way around.


The solution is simply to add the annotation:

import javax.xml.bind.annotation.XmlTransient;

@XmlTransient

at the getter of the property that causes the cycle.

0

精彩评论

暂无评论...
验证码 换一张
取 消