Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:17:08+00:00 2026-06-16T04:17:08+00:00

I am using Hibernate and RESTeasy, i try to avoid a cycle with these

  • 0

I am using Hibernate and RESTeasy, i try to avoid a cycle with these entities, as i have a OneToMany (ManyToOne) bidirectionnal relation between Artiste and Oeuvre entities :

Oeuvre.java

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@XmlRootElement(name = "oeuvre")
public abstract class Oeuvre {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Embedded
    private Dimension dimension;

    @XmlElement(defaultValue = "true")
    private boolean hasBeenReproduced;

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "artiste_id")
    @XmlIDREF
    private Artiste artiste;

    @XmlElement
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    // @XmlTransient
    @XmlInverseReference(mappedBy = "oeuvres")
    public Artiste getArtiste() {
        return artiste;
    }

    public void setArtiste(Artiste artiste) {
        this.artiste = artiste;
        artiste.addOeuvre(this);
    }

}

Personne.java

import javax.persistence.*;
import javax.xml.bind.annotation.XmlID;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Personne {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @XmlID
    private int id;

}

Artiste.java

import java.util.*;
import javax.persistence.*;
import javax.xml.bind.annotation.*;

@Entity
@XmlRootElement(name = "artiste")
public class Artiste extends Personne {

    private String bibliographie;

    @OneToMany(mappedBy = "artiste", orphanRemoval = true, cascade = {
            CascadeType.PERSIST, CascadeType.REMOVE })
    private List<Oeuvre> oeuvres = new ArrayList<Oeuvre>();

    @XmlElement
    public List<Oeuvre> getOeuvres() {
        return oeuvres;
    }

    public void setOeuvres(List<Oeuvre> oeuvres) {
        this.oeuvres = oeuvres;
    }

}

So i decided to use MOXy,

Here is my POM

<repository>
        <id>EclipseLink</id>
        <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
</repository>
<dependency>
       <groupId>org.eclipse.persistence</groupId>
       <artifactId>org.eclipse.persistence.moxy </artifactId>
       <version>2.3.2</version>     
    </dependency>

nb : i would like to only have org.eclipse.persistence.moxy-2.3.2.jar as i am using hibernate (i don’t want eclipseLink), but i also have 3 other jars (including the core)

Then i put a jaxb.properties file in the package of my entities :

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

And added @XmlInverseReference(mappedBy=”oeuvres”) to getArtiste() instead od @XmlTranscient
==> i don’t have cycle anymore (like xmlTranscient) but i still don’t have any back pointer.

Then i added @XmlID & @XmlIDREF, the id of the artist is now represented in the xml result of a work of art BUT it doesn’t have the good value (0 but shoud be something else)

<Oeuvre>
  <hasBeenReproduced>false</hasBeenReproduced>
  <artiste>0</artiste>
  <year>2010</year>
  <id>2</id>
  <titre>La joconde</titre>
</Oeuvre>

What am i doing wrong ? Thx in advance

EDIT :

Ok i have the following output using @XmlInverseReference when i marshall an “Artiste” Object :

<artiste>
  <id>1</id>
  <nom>a</nom>
  <prenom>b</prenom>
  <oeuvres>
     <hasBeenReproduced>false</hasBeenReproduced>
     <year>2010</year>
     <id>25</id>
     <titre>La joconde</titre>
  </oeuvres>
</artiste>    

According to your example this is the correct behaviour.
So if i understand well, it’s not possible to have a reference of the artiste id in the “Oeuvre” output (given above). We can’t retrieve the artist from a work of art.
In my case i don’t have to use @XmlID ?

Thx for your complete answer Blaise Doughan, it is much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:17:08+00:00Added an answer on June 16, 2026 at 4:17 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    Below is a simplified example of how to use MOXy’s @XmlInverseReference extension.

    Foo

    Foo is the root object for this example.

    package forum13957068;
    
    import java.util.List;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Foo {
    
        String fooProp;
    
        @XmlElement(name="bar")
        List<Bar> bars;
    
    }
    

    Bar

    Bar is a child object. We use the @XmlInverseReference annotation to populate the foo field with a reference to the parent object.

    package forum13957068;
    
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Bar {
    
        private String barProp;
    
        @XmlInverseReference(mappedBy="bars")
        Foo foo;
    
    }
    

    jaxb.properties

    To specify MOXy as your JAXB provider you need to include a file called jaxb.properties with the following entry:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    input.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <foo>
        <fooProp>A</fooProp>
        <bar>
            <barProp>B</barProp>
        </bar>
        <bar>
            <barProp>B</barProp>
        </bar>
    </foo>
    

    Demo

    The following demo code will unmarshal the XML, check the back pointer and then marshal the objects back to XML.

    package forum13957068;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Foo.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum13957068/input.xml");
            Foo foo = (Foo) unmarshaller.unmarshal(xml);
    
            for(Bar bar : foo.bars) {
                System.out.println(bar.foo == foo);
            }
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(foo, System.out);
        }
    
    }
    

    Output

    Below is the output of running the demo code. Node how the back pointer is populated correctly.

    true
    true
    <?xml version="1.0" encoding="UTF-8"?>
    <foo>
       <fooProp>A</fooProp>
       <bar>
          <barProp>B</barProp>
       </bar>
       <bar>
          <barProp>C</barProp>
       </bar>
    </foo>
    

    MOXy and Maven

    You can use the following in your pom.xml file to pull in MOXy.

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>2.4.1</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>EclipseLink Repo</id>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
        </repository>
    </repositories>
    

    For some example POM files that pull in MOXy check out the examples I have hosted on GitHub:

    • https://github.com/bdoughan

    UPDATE

    According to your example this is the correct behaviour. So if i
    understand well, it’s not possible to have a reference of the artiste
    id in the “Oeuvre” output (given above). We can’t retrieve the artist
    from a work of art. In my case i don’t have to use @XmlID ?

    Below I’ve provided an alternate mapping that uses @XmlID/@XmlIDREF instead of @XmlInverseReference.

    Foo

    package forum13957068;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Foo {
    
        @XmlID
        String id;
    
        String fooProp;
    
        @XmlElement(name="bar")
        List<Bar> bars;
    
    }
    

    Bar

    package forum13957068;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Bar {
    
        private String barProp;
    
        @XmlIDREF
        Foo foo;
    
    }
    

    input.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <foo>
        <id>1</id>
        <fooProp>A</fooProp>
        <bar>
            <foo>1</foo>
            <barProp>B</barProp>
        </bar>
        <bar>
            <foo>1</foo>
            <barProp>B</barProp>
        </bar>
    </foo>
    

    For More Information

    • http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Hibernate 4.0 I have three hibernate entities: Song, CoverArt, CoverImage Songs represents music
Using Hibernate 4.1.1.Final. When I try to add @ManyToOne, schema creation fails with: org.hibernate.MappingException:
I'm using RestEasy and hibernate to return response in Jackson. I have a bean
I'm using Hibernate as ORM and I have my DAOs annotated with @Repository .
I have a webservice using Hibernate as DAL - using MySql with InnoDB. Since
I am using hibernate. I have written native SQL query and I would like
I am using hibernate. i have read some where about Example.create. what is it
I am using Hibernate to map with MySQL I have an entity class in
I've been using Hibernate for a few years but have only used it with
Using hibernate @Any mapping to map multiple entities in the same, I haven't be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.