0

How to annotate and use a java persistent object in a onetomany bidirectional relationship so that the entity can be converted to its XML representation which when taken up by a restful client can be be converted back to an entity object again

kharesoft
  • 35
  • 1
  • 1
  • 8

1 Answers1

0

Here is a REAL code from one of my projects which probably (if I got it right) does exactly what you need.

@Entity
@Table(name = "KIOSK")
@XmlRootElement
public class RealKiosk implements Kiosk {

  private List<Device> kioskDevices = new ArrayList<Device>();

  @OneToMany(fetch = FetchType.LAZY, targetEntity = DeviceImpl.class, mappedBy = "kiosk", cascade = CascadeType.ALL)
  @XmlElement(type = DeviceImpl.class)
  public List<Device> getKioskDevices() {   
    return kioskDevices;
  }

  public void setKioskDevices(List<Device> kioskDevices) {    
    this.kioskDevices = kioskDevices;
  }
}

In rare cases you would use

@XmlAnyElement(lax = true)

instead of

@XmlElement(type = DeviceImpl.class)

But if you are not using interfaces and just using classes only

@XmlRootElement

should be sufficient.

But all of that is relevant if you are not using Jackson with Spring for example. If so that would be a little bit different story.

goroncy
  • 2,053
  • 1
  • 19
  • 16