1

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

这是我的班级 MAAccount 实现:

@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

4

1 回答 1

1

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

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

于 2011-02-17T18:03:40.060 回答