2016-10-18 1 views
0

jsonのシリアル化/逆シリアル化を使用してデータを格納/取得するエンティティモデルを使用します。 遅延読み込みデータをデシリアライズする際に問題が発生します。ジャーマン・アノテーション(または他の方法)を使用してデシリアライズで複雑なデータを取得する方法

つまり、私は自分のエンティティ、スーパークラス、このようなインターフェイスを定義しました。

ATGクラス

@Entity 
@Table(name = "atg", uniqueConstraints = @UniqueConstraint(columnNames = "code")) 
public class ATG extends ParentEntity { 

    @JsonInclude(JsonInclude.Include.NON_EMPTY) 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "companyid") 
    Manufacturer manufacturer; 

    getManufacturer(); 
    setManufacturer();  
} 

Customerクラス

@Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
public class Customer implements HasCustomer { 

    @JsonInclude(JsonInclude.Include.NON_EMPTY) 
    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY) 
    @JoinColumn(name = "customer2_id", nullable = true) 
    private Customer customer; 

    getCustomer(); 
    setCustomer(); 
} 

ParentEntity

@MappedSuperclass 
public abstract class ParentEntity implements HasCustomer{ 

    @JsonInclude(JsonInclude.Include.NON_EMPTY) 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "customer2_id", nullable = false) 
    private Customer customer; 

    getCustomer(); 
    setCustomer(); 
} 

のHasCustomerインタフェース

public interface HasCustomer extends Serializable{ 

    public Customer getCustomer(); 

    public void setCustomer(final Customer customer); 

    public Long getCustomerId(); 

    public void setCustomerId(final Long customerId); 
} 

Customerクラスは、顧客オブジェクトへの参照(論理的要件)を有します。

データベースからデータを取得するために、サービスエンドポイントにREST呼び出しを行います。 Ideはfetch = FetchType.LAZYという複雑なdataTypesを罰金しました。 ユーザーが本当に応答のあるデータを必要とする場合は、ユーザーが指定します。したがって、バックエンドではFetchMode.JOINを設定してデータを取得します。 (これは動作します)。

問題は、Json注釈のみです。この場合、注釈をどのように指定する必要がありますか?私は1つのレベルでjsonIgnoreを続ける場合 、それは結果をブロックしたり、私は結果にユーザーが変数に複雑なデータ私は、すべてのゲッター、セッターに@jsonIgnore/@jsonproperty注釈を試してみました

とフェッチするように要求されていないすべての時間を参照してください定義。 (私はすべての組み合わせを試したと信じています:)

しかし、誰も希望の結果を返していません。

私はATGサービスへの呼び出し

誰も私を助けることができるの際顧客データと製造データを取得するために適切な注釈を維持したいですか?

私はjackson(com.fasterxml.jackson)V 2.6.3 hibernate v 5.2.1.Finalを使用します。

+0

でそれを使用する "望ましい結果" とは何ですか? 100%明らかではありませんので、JSONとして、または「プロパティ 'customer'を持たない」のように説明することができます。 – StaxMan

答えて

0

私はこのようにジャクソンを使用すべきかどうかはわかりませんが、これが役立つかもしれません。

顧客シリアライザに

public class CustomerSerializer extends JsonDeserializer<Customer> { 

    public Customer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { 
     .... 
    } 
} 

を定義します次に、あなたのインターフェース

@JsonDeserialize(using = CustomerSerializer.class) 
public class Customer implements HasCustomer { 
+0

このソリューションは、毎回「顧客」を提供します。私のcustomJsondeserilizerクラスはまったく呼び出されないようです。 – Ratha

関連する問題