2011-02-11 76 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