1

Eclipselink MOXy を使用して POJO (JPA を使用) を json に変換しました。そして仕事です。しかし、私には1つの問題があります。pojo クラス MAccount には、クラス MProduct との多対 1 の関係が含まれています。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"}}]
}

誰でもこの問題を解決する方法を知っていますか

ありがとうございます

4

1 に答える 1

1

フィールドに注釈を付けているため、遅延読み込みのために JPA がまだそのフィールドにデータを入力していない可能性があります。代わりにプロパティ (get/set) に注釈を付けた場合でも、この動作は見られますか?

@XmlInverseReference の詳細については、次を参照してください。

于 2011-02-17T18:03:40.060 に答える