Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @XmlInverseReference with interface field
@XmlInverseReference with interface field [message #1020523] Mon, 18 March 2013 11:17 Go to next message
irina adam is currently offline irina adam
Messages: 4
Registered: March 2013
Junior Member
Hello,

I was wondering if the @XmlInverseReference also works on entities with fields that refer to an interface since the @XmlElement gives an instantiation error for those types of fields.

I am using MOXy with Spring and follow the instructions here http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Spring/JAXBAnnotation. I have for example a Sensor class that has a SensorReading field, which is an interface.
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "sensor")
@PrimaryKeyJoinColumn(name = "sensor_id")
public class Sensor extends LowLevelEntity implements Serializable
{

	@XmlInverseReference(mappedBy = "sensor")
	@OneToOne(targetEntity = ConcreteSensorReading.class, cascade = CascadeType.ALL,
			fetch = FetchType.EAGER, orphanRemoval = true)
	@JoinColumn(name = "concrete_sensor_reading_id")
	protected SensorReading sensorReading;
// getters and setters
}




The ConcreteSensorReading class is an implementatition of the SensorReading interface
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "concrete_sensor_reading")
public class ConcreteSensorReading implements SensorReading
{
	
	@XmlID
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "concrete_sensor_reading_id")
	private long uuid;
	
	@XmlInverseReference(mappedBy = "sensorReading")
	@XmlElement
	@OneToOne(mappedBy = "sensorReading", fetch = FetchType.EAGER )
	protected Sensor sensor;

// getters and setters
}

The SensorReading interface is annotated like so

@XmlRootElement
@XmlSeeAlso(ConcreteSensorReading.class)
public interface SensorReading
{	
	public Sensor getSensor();	
	public void setSensor(Sensor sensor);	
}


Using this configuration I get no errors but the ConcreteSensorReading is not mapped to the xml when I marshall the Sensor object using the XmlHelper save method. Is there a correct way of doing this? I'm very new to MOXy and I might not have understood how this must be done. Thank you for your help.
Re: @XmlInverseReference with interface field [message #1021246 is a reply to message #1020523] Tue, 19 March 2013 18:19 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

You could do the following:

Sensor

Using EclipseLink 2.5.0 (you can get a nightly build from: http://www.eclipse.org/eclipselink/downloads/nightly.php) you could use an @XmlInverseReference with an @XmlElement to specify the concrete type.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "sensor")
@PrimaryKeyJoinColumn(name = "sensor_id")
public class Sensor extends LowLevelEntity implements Serializable
{

    @XmlInverseReference(mappedBy = "sensor")
    @XmlElement(type=ConcreteSensorReading.class)
    @OneToOne(targetEntity = ConcreteSensorReading.class, cascade = CascadeType.ALL,
            fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "concrete_sensor_reading_id")
    protected SensorReading sensorReading;

    // getters and setters

}


ConcreteSensorReading

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "concrete_sensor_reading")
public class ConcreteSensorReading implements SensorReading
{
    
    @XmlID
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "concrete_sensor_reading_id")
    private long uuid;
    
    @XmlInverseReference(mappedBy = "sensorReading")
    @XmlElement
    @OneToOne(mappedBy = "sensorReading", fetch = FetchType.EAGER )
    protected Sensor sensor;

    @Override
    public Sensor getSensor() {
        return sensor;
    }

    @Override
    public void setSensor(Sensor sensor) {
        this.sensor = sensor;
    }

    // getters and setters

}


SensorReading

You shouldn't have any mappings on the interface itself (see: http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html).

public interface SensorReading
{   
    public Sensor getSensor();  
    public void setSensor(Sensor sensor);   
}


For More Information
- http://blog.bdoughan.com/2013/03/moxys-xmlinversereference-is-now-truly.html
Re: @XmlInverseReference with interface field [message #1021497 is a reply to message #1021246] Wed, 20 March 2013 08:22 Go to previous message
irina adam is currently offline irina adam
Messages: 4
Registered: March 2013
Junior Member
Thank you very much for the detailed answer! Everything works now.
Previous Topic:EclipseLink adding extra parens which postgreSQL doesn't like
Next Topic:MOXy point release?
Goto Forum:
  


Current Time: Thu Sep 18 07:53:04 GMT 2014

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

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