Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @XmlInverseReference back pointer not set on unmarshalling
@XmlInverseReference back pointer not set on unmarshalling [message #1022701] Fri, 22 March 2013 11:34 Go to next message
irina adam is currently offline irina adamFriend
Messages: 4
Registered: March 2013
Junior Member
I have a Spring project and want to use MOXy to unmarshall objects from an xml file. I am using the night build from Eclipselink 2.5 from 15.03.2013. My classes are:

Location class

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Home.class, Room.class})

@Entity
@Table(name = "location")
@Inheritance(strategy = InheritanceType.JOINED)
public class Location implements Serializable
{
	@XmlTransient
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "location_id", unique = true)
	private long uuid;
	
	@XmlID
	@Column(name = "name", unique = true)
	protected String name;

        public Location(String name)
	{	setName(name);	}
	// other fields and getters/setters




Home class


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "home")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "home_id")


public class Home extends Location implements Serializable
{
        @XmlElement(name = "room")
	@XmlElementWrapper( name = "rooms")
	@OneToMany(mappedBy = "containingHome", cascade = {CascadeType.ALL}, orphanRemoval = true)
	private Set<Room> rooms;

        public Home(String name)
	{
		super(name);
		init();
	}
	
	private Home() {init();}
	
	private void init()
	{
		setRooms(new HashSet<Room>());
	}
   // getters setters
}



Room class


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "room")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "room_id")
public class Room extends Location implements Serializable
{
	@XmlInverseReference(mappedBy="rooms")
	@ManyToOne()
	@JoinColumn(name = "home_id")
	protected Home containingHome;

        public Room(String name, Location containingHome)
	{
		super(name);
		setContainingHome((Home)containingHome);
	}

  // getters setters
}


I am using the configuration for spring found here : http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Spring/JAXBAnnotations and my test is:

@Test 
	public void marshallingHomeTest()
	{
		Home home = new Home(locationName);
		Room room = new Room("room", home);
	
		try
		{
			logger.info("marshalling home " + home);
			xmlHelper.save(home, new StreamResult(new FileOutputStream("homeconfig.xml")));
			
			home = (Home) xmlHelper.load(new StreamSource(new FileInputStream("homeconfig.xml")));
			logger.info("unmarshalled home " + home);
			xmlHelper.save(home, new StreamResult(new FileOutputStream("homeconfig2.xml")));
			
		} catch (XmlMappingException e)
		{
			e.printStackTrace();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}



The result is:

JAXBMarshallTest:302 - marshalling home model.location.Home@6cdd4cfa[locationId=0,name=home1,rooms=[model.location.Room@34e8e8ec[roomType=UNDEFINED,containingHome=home1,locationId=0,name=room]]]
XMLHelper:31 - saving object to xml file
XMLHelper:23 - loading object from xml source
JAXBMarshallTest:306 - unmarshalled home model.location.Home@2a5c0ab8[locationId=0,name=home1,rooms=[]]


The xml file when saving it the first time is ok, but the second one contains no rooms. It is very strange since I have used this before and it worked, the rooms got populated in the set but now it doesn't anymore. I reverted to the previous 2.4 version of org.eclipse.persistence but the result is the same. Am I doing something wrong with the mapping? Do I need to look out for something in particular when creating the objects?
Re: @XmlInverseReference back pointer not set on unmarshalling [message #1022863 is a reply to message #1022701] Fri, 22 March 2013 17:02 Go to previous message
irina adam is currently offline irina adamFriend
Messages: 4
Registered: March 2013
Junior Member
Ok, so I figured out what was happening and it was due to the fact that I did not initialize some of the fields of the Location class when creating the objects. I had DateTime fields of the form

@XmlJavaTypeAdapter(type=org.joda.time.DateTime.class, 
			value=utility.xml.XmlDateTimeAdapter.class)
	@Column(name = "abnormal_interval_start")
	@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
	protected DateTime intervalStart;


that I thought I could leave uninitialized for a simple test and because of that the unmarshalling did not work. The adapter class is taken from the excellent blog of Blaise Doughan about jaxb and joda time. I will leave the message though if ever someone forgets to initialize fields Smile
Previous Topic:StuckThreadMaxTime in weblogic
Next Topic:Scrollable ResultSet Exception "End of Stream"
Goto Forum:
  


Current Time: Thu Apr 25 20:16:00 GMT 2024

Powered by FUDForum. Page generated in 0.03487 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top