莫西。生成JSON,不包含引用类

时间:2011-02-11 11:16:59

标签: json eclipselink moxy

我使用Eclipselink MOXy将我的POJO(使用JPA)转换为json。这是工作。 但我有一个问题。我有pojo类MAccount包含类MProduct的多对一关系。当我转换为json时,结果显示类MAccount不在类MProduct中。

这是我的班级MAccount实施:

@XmlRootElement
@Entity
@Table(name="m_account")
public class MAccount extends BaseObject implements Serializable {
    private static final long serialVersionUID = UUID.randomUUID().getMostSignificantBits();

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @XmlID
    private Long id;

    @Column(name="account_id")
    private String accountId;

    @Column(name="card_number")
    private String cardNumber;

    //bi-directional many-to-one association to Product
    @ManyToOne
    @JoinColumn(name="m_product_id")
    @XmlIDREF
    private MProduct mProduct;

    public MCustomerAccount() {
    }   

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAccountId() {
        return this.accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public MProduct getMProduct() {
        return this.mProduct;
    }

    public void setMProduct(MProduct mProduct) {
        this.mProduct = mProduct;
    }

    // Imlement base object method
    ...
}

这里是我的类MProduct实现:

@XmlRootElement
@Entity
@Table(name="m_product")
public class MProduct extends BaseObject implements Serializable {
    private static final long serialVersionUID = UUID.randomUUID().getMostSignificantBits();

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @XmlID
    private Long id;

    @Column(name="product_code")
    private String productCode;

    @Column(name="product_name")
    private String productName;

    //bi-directional many-to-one association to MAccount
    @OneToMany(mappedBy="mProduct") 
    @XmlInverseReference(mappedBy="mProduct")
    private Set<MAccount> mAccountList;

    public MProduct() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getProductCode() {
        return this.productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return this.productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Set<MAccount> getMAccountList() {
        return this.mAccountList;
    }

    public void setMAccountList(Set<MAccount> mAccountList) {
        this.mAccountList = mAccountList;
    }

    // Imlement base object method
    ...
}

从MAccount类

生成JSON
{"MAccount":[
    {"@type":"mAccount","id":"6","accountId":"05866039901"},
    {"@type":"mAccount","id":"7","accountId":"25600036290"}]
}

那里没有MProduct,正确的json结果应该如下所示

{"MAccount":[
    {"@type":"mAccount","id":6,"accountId":"05866039901","MProduct":{"@type":"mProduct","productCode":"T01","productName":"Book"}},
   {"@type":"mAccount","id":7,"accountId":"25600036290","MProduct":{"@type":"mProduct","productCode":"T02","productName":"Pen"}}]
}

有人知道如何解决这个问题

感谢b4

1 个答案:

答案 0 :(得分:1)

因为您正在注释该字段,所以由于延迟加载,JPA可能尚未填充该字段。如果您注释属性(获取/设置)而不是仍然看到此行为?

有关@XmlInverseReference的更多信息,请参阅: