JAXB:在对象图中检测到循环

时间:2017-08-30 15:11:26

标签: java jaxb

我有两个实体。类别实体:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "EPBS_DATA.NEWS_CATEGORY")
@XmlType(propOrder = { "id", "name", "news" })
public class CategoryEntity {
    public final static String ID_GENERATOR_NAME =
    "EPBS_DATA.sq_news_category";
    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
                generator = ID_GENERATOR_NAME)
    @SequenceGenerator(name = ID_GENERATOR_NAME,
                   sequenceName = ID_GENERATOR_NAME, allocationSize = 1)
    private long id;

    @Column(name = "CATEGORY_NAME", nullable = false, length = 1024)
    private String name;
    @ManyToMany(mappedBy = "categories")
    @XmlElementWrapper(name = "news")
    @XmlElement(name = "")
    private List<NewsEntity> news = new ArrayList<NewsEntity>();

    setters/getters
}

和NewsEntity

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "EPBS_DATA.NEWS")
@XmlType(propOrder =
     { "id", "body", "lead", "creatorId", "date", "federal", "regId",
       "raw", "file", "title", "editDate", "fixed", "categories",
       "publishDates" })
public class NewsEntity {
    public final static String ID_GENERATOR_NAME = "EPBS_DATA.SQ_EPBS";
    public final static String GET_NEWS_BY_DATE = "NewsEntity.getNewsByDate";
    public final static String GET_NEWS_COUNT = "NewsEntity.getNewsCount";
    public final static String GET_DATES = "NewsEntity.getDates";
    @Id
    @Column(name = "NEWS_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,
                generator = ID_GENERATOR_NAME)
@SequenceGenerator(name = ID_GENERATOR_NAME,
                   sequenceName = ID_GENERATOR_NAME, allocationSize = 1)
    private long id;
    @Column(name = "NEWS_BODY", nullable = true)
    @Lob
    private String body;
    @ManyToMany
    @JoinTable(name = "EPBS_DATA.NEWS_CATEGORY_ASSIGMENT",
           joinColumns = @JoinColumn(name = "ID_NEWS",
                                     referencedColumnName = "NEWS_ID"),
           inverseJoinColumns =
           @JoinColumn(name = "ID_CATEGORY", referencedColumnName = "ID"))
    @XmlInverseReference(mappedBy = "news")
    @XmlElementWrapper(name = "categories")
    @XmlElement(name = "")
    private List<CategoryEntity> categories = new ArrayList<CategoryEntity>();

我有错误在对象图中检测到一个循环。这将导致无限深的XML:jpa.news.CategoryEntity@79c7b9d3 - &gt; jpa.news.NewsEntity@31453ead - &gt; jpa.news.CategoryEntity@79c7b9d3]

我尝试使用@XmlTransient和@XmlInverseReference,但无论我有什么错误。

1 个答案:

答案 0 :(得分:1)

您的bean互相引用:CategoryEntity.news包含至少一个NewsEntity,其属性类别包含上述CategroyEntity。试图将其作为XML输出将导致无穷无尽的循环。取决于您的&#34;领先&#34;实体是您必须将类别或新闻标记为@XmlTransient。