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?