Question

I used Eclipselink MOXy to convert my POJO(using JPA) to json. and it's work. but i have one problem. I have pojo class MAccount contain many to one relation to class MProduct,. when I convert to json, result show that class MAccount not in class MProduct.

here my class MAccount implementation:

@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
    ...
}

here my class MProduct implementation:

@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
    ...
}

And generate JSON from MAccount class

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

there is no MProduct in there, the correct json result should be like below

{"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"}}]
}

Is Anyone know how to solve this problem

Thank's b4

Was it helpful?

Solution

Because you are annotating the field, there is a chance that JPA has not populated that field yet due to lazy loading. If you annotate the property (get/set) instead do you still see this behaviour?

For more information on @XmlInverseReference see:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top