Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jpa entities over a JAX WS services without infinite loop

How can I send JPA generated entities over an JAX WS web service without getting the an XML infinite cycle exception because of the cycle of references in those entities?

Any idea? I found this MOXy that can do it...partially. But i already have the entities generated and to manually add XmlTransient and such annotations to each of them it's crazy.

Do you have any other idea how to do it?

Thanks!

like image 818
Andr Avatar asked Nov 06 '22 05:11

Andr


1 Answers

EclipseLink JAXB (MOXy) can handle this with its bidirectional mapping with @XmlInverseReference:

import javax.persistence.*;

@Entity
public class Customer {

    @Id
    private long id;

    @OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
    private Address address;

}

and

import javax.persistence.*;
import org.eclipse.persistence.oxm.annotations.*;

@Entity
public class Address implements Serializable {

    @Id
    private long id;

    @OneToOne
    @JoinColumn(name="ID")
    @MapsId
    @XmlInverseReference(mappedBy="address")
    private Customer customer;

}

For more information see:

  • http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA

You can also use MOXy's externalized representation of the metadata for this. For more information see:

  • XML to Java mapping tool - with mapping descriptor
  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
like image 133
bdoughan Avatar answered Nov 09 '22 12:11

bdoughan