You can use the XmlAdapter
for this use case:
package forum7278406; import javax.xml.bind.annotation.adapters.XmlAdapter; public class TargetAdapter extends XmlAdapter<Long, Target> { @Override public Long marshal(Target target) throws Exception { return target.getId(); } @Override public Target unmarshal(Long id) throws Exception { Target target = new Target(); target.setId(id); return target; } }
XmlAdapter
registered in the Dependency
class using the @XmlJavaTypeAdapter
annotation:
package forum7278406; import javax.persistence.*; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @Entity @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Dependency { @Id @GeneratedValue private Long id; @ManyToOne(optional=false) @Column(name="target_id") @XmlJavaTypeAdapter(TargetAdapter.class) private Target target; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Target getTarget() { return target; } public void setTarget(Target target) { this.target = target; } }
Further
Instead of creating a new Target
instance Target
we could use the EntityManager
to query the corresponding instance from the database. Our XmlAdapter
will be resized to look something like this:
package forum7278406; import javax.persistence.EntityManager; import javax.xml.bind.annotation.adapters.XmlAdapter; public class TargetAdapter extends XmlAdapter<Long, Target> { EntityManager entityManager; public TargetAdapter() { } public TargetAdapter(EntityManager entityManager) { this.entityManager = entityManager; } @Override public Long marshal(Target target) throws Exception { return target.getId(); } @Override public Target unmarshal(Long id) throws Exception { Target target = null; if(null != entityManager) { target = entityManager.find(Target.class, id); } if(null == target) { target = new Target(); target.setId(id); } return target; } }
Now, to install an EntityManager
instance on our XmlAdapter
, we can do the following:
Unmarshaller umarshaller = jaxbContext.createUnmarshaller(); TargetAdapter targetAdatper = new TargetAdapter(entityManager); unmarshaller.setAdapter(targetAdapter);
source share