通过其ID来序列化JAXB对象?

时间:2011-09-02 01:29:18

标签: java xml hibernate jaxb

在我的数据模型中,我有这样的效果:

@Entity
public class Target {

    @Id 
    @GeneratedValue
    private Long id;

    /* ...etc... */
}

@Entity
public class Dependency {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(optional=false)
    @Column(name="target_id")
    private Target target;

    /* ...etc... */
}

我已经将Target序列化了,但我需要序列化Dependency。基本上,我需要的是这样的东西:

<dependency>
    <id>100</id>
    <targetId>200</targetId>
</dependency>

有没有办法在JAXB注释中执行此操作而不修改我的模型?

2 个答案:

答案 0 :(得分:7)

您可以在此用例中使用XmlAdapter

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使用Dependency注释在@XmlJavaTypeAdapter班级注册:

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;
    }

}

进一步

我们可以使用Target从数据库中查询相应的实例,而不只是创建EntityManager的新实例。我们的XmlAdapter会更改为:

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;
    }

}

现在要在EntityManager上设置XmlAdapter的实例,我们可以执行以下操作:

Unmarshaller umarshaller = jaxbContext.createUnmarshaller();
TargetAdapter targetAdatper = new TargetAdapter(entityManager);
unmarshaller.setAdapter(targetAdapter);

答案 1 :(得分:2)

适用于带有XmlID和XmlIDRef的EclipseLink MOXy(但是对于sun JAXB失败,其中XmlID必须是字符串)

@Entity
@XmlRootElement
public class Target {
    @Id
    @GeneratedValue
    @XmlID
    @XmlElement
    private Long id;
}


@Entity
@XmlRootElement
public class Dependency {

    @Id
    @GeneratedValue
    @XmlElement
    private  Long id;

    @ManyToOne(optional = false)
    @Column(name = "target_id")
    @XmlIDREF
    @XmlElement(name = "targetId")
    private Target target;
}