2011-02-11 72 views
1

我用Eclipselink MOXy將我的POJO(使用JPA)轉換爲json。這是工作。 但我有一個問題。我有pojo類MAccount包含與MProduct類的多對一關係。當我轉換爲json時,結果顯示類MAccount不在類MProduct中。MOXy。生成JSON,不包含引用類

這裏我班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實現:從MAccount類

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

沒有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 
    ... 
} 

,並生成JSON,正確的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

因爲你標註的領域,有一個機會,JPA還沒有填充的領域尚未因延遲加載。如果你註解了屬性(get/set),你是否仍然看到這種行爲?

有關@XmlInverseReference更多信息,請參見:

+0

將在解決方案看起來像註釋的性質是什麼,而不是? – 2014-04-10 18:56:19