java - Serialize a JAXB object via its ID? -


In my data model, I have something for this effect: @Entity Local Class Goal {@Id @GeneratedValue Private Long ID; / * ... etc ... * @} Entity public class dependency {@Id @GeneratedValue Private Long ID; @ManyToOne (optional = false) @ column (name = "target_id") personal target target; / * ... etc ... * /}

I'm already sorting target , but I have to serialize dependence . Essentially, what I want is something like this:

  & lt; Dependency & gt; & Lt; ID & gt; 100 & lt; / Id & gt; & Lt; Targetid & gt; 200 & lt; / Targetid & gt; & Lt; / Dependencies & gt;   

Is there a way to do this in JAXB annotations without modifying my model?

You can use the XmlAdapter for the case of this use:

  Package forums 7278406; Import javax.xml.bind.annotation.adapters.XmlAdapter; Public Category TargetAdapter Extends XmlAdapter & lt; Long, Goal & gt; {@ Override Public Long Marshall (goal target) throws exception {return target.getId (); } @ Override Public Target throws exceptional (Long Id) {target goal = new target (); Target.setId (id); Return target; }}   

XmlAdapter dependency on @XmlJavaTypeAdapter annotation :

  Package forums 7278406; Import javax.preistence *; Import javax.xml.bind.annotation *; Import javax.xml.bind.annotation.adapters.xmlJavaTypeAdapter; @ Entity @ XMLRealment @ XMLAccessType (XmlAccessType.FIELD) Public class dependency {@Id @GeneratedValue Private Long ID; @ManyToOne (optional = false) @column (name = "target_ID") @ XML Java style adapter (target adapter) private target target; Public long iid () {return id; } Public Zero Set ID (Long ID) {this.id = id; } Public Target GetTarget () {Return Target; } Public Zero Set Goals (Goal Goals) {this.target = Target; }     

Going forward

Instead we create a new example of target You can use a EntityManager for the query of the same example from the database. Our XmlAdapter will be changed to look something like this:

  Package Forum 7278406; Import javax.preistence.EntityManager; Import javax.xml.bind.annotation.adapters.XmlAdapter; Public Category TargetAdapter Extends XmlAdapter & lt; Long, Goal & gt; {EntityManager entityManager; Public TargetAdapter () {} Public TargetAdapter (EntityManager entityManager) {this.entityManager = entityManager; } @ Override Public Long Marshal (target goal) throws exception {return target.getId (); } @ Override Public Target throws exceptional (long id) {target goal = zero; If (zero! = Unit Manager) {target = entityManager.find (Target.class, id); } If (null == target) {target = new target (); Target.setId (id); } Return targets; }} To set the example of  EntityManager  on our  XmlAdapter , we can do the following:  
  Unmashshallar Ursshaler = Jacob Contex. Siquitaminer (); TargetAptactor TargetEdapter = New TargetAudience (Unit Manager); Unmarshaller.setAdapter (targetAdapter);    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

memcached - Django cache performance -

java : convert string value to int -