2016-08-20 4 views
2

私は映画のリストと監督オブジェクトを含むMovieクラスを含むDirectorクラスを持っています。hibernate - いくつかのプロパティが取得されないようにします。

問題は、映画をフェッチすると、対応するDirectorもフェッチされ、その結果、そのDirectorによって監督されたムービーのリストも返されます。 ?

私の質問どのように私はここに

(私はまだ私は一人でディレクターをフェッチするとき映画のリストを取得したい)私は映画をフェッチするたびにフェッチされてから、映画のリストを防ぐことができているが、私のディレクタークラスです:

@Entity 
@Table(name = "DIRECTOR") 
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") 
public class Director { 

    @Id 
    @Column(name = "ID", nullable = false) 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private String id; 


    @NotNull 
    @Column(name = "D_NAME", nullable = false) 
    private String name; 

    @NotNull 
    @Column(name = "LOCATION", nullable = false) 
    private String location; 

    @OneToMany(fetch = FetchType.EAGER) 
    @JoinColumn(name = "director_id") 
    private List<Movie> movies; 

    @Column(name = "POSITIONS") 
    @Enumerated(EnumType.ORDINAL) 
    private Position positions; 
    @Column(name = "PHOTO_URL") 
    private String photoUrl; 

    //setters and getters 
} 

とサンプルが返されるJSON:

{ 
    "id": "christopher-nolan", 
    "name": "Christopher Nolan", 
    "location": "London, England, UK", 
    "movies": [ 
    { 
     "id": "inception", 
     "title": "Inception", 
     "rate": 8.8, 
     "numberOfRates": 1470268, 
     "categories": "ACTION", 
     "director": "christopher-nolan", 
     "duration": 123, 
     "photoUrl": "http://cdn.persiangig.com/preview/Ku3leEm3N7/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw%40%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO." 
    }, 
    { 
     "id": "interstellar", 
     "title": "Interstellar", 
     "rate": 8.6, 
     "numberOfRates": 930496, 
     "categories": "ADVENTURE", 
     "director": "christopher-nolan", 
     "duration": 169, 
     "photoUrl": "http://cdn.persiangig.com/preview/tSqxSLMKg5/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival." 
    }, 
    { 
     "id": "the-dark-knight", 
     "title": "The Dark Knight", 
     "rate": 9, 
     "numberOfRates": 1678434, 
     "categories": "ACTION", 
     "director": "christopher-nolan", 
     "duration": 152, 
     "photoUrl": "http://cdn.persiangig.com/preview/SyC1yqkQ55/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw%40%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice." 
    }, 
    { 
     "id": "the-dark-knight-rises", 
     "title": "The Dark Knight Rises", 
     "rate": 8.5, 
     "numberOfRates": 1146075, 
     "categories": "ACTION", 
     "director": "christopher-nolan", 
     "duration": 164, 
     "photoUrl": "http://cdn.persiangig.com/preview/UTo1oyUVDH/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw%40%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "Eight years after the Joker's reign of anarchy, the Dark Knight, with the help of the enigmatic Selina, is forced from his imposed exile to save Gotham City, now on the edge of total annihilation, from the brutal guerrilla terrorist Bane." 
    } 
    ], 
    "positions": "DIRECTOR", 
    "photoUrl": "http://cdn.persiangig.com/preview/xNYollZv1Y/MV5BNjE3NDQyOTYyMV5BMl5BanBnXkFtZTcwODcyODU2Mw%40%40._V1_UY317_CR7%2C0%2C214%2C317_AL_.jpg" 
} 

と私Movieクラス:

@Entity 
@Table(name = "MOVIE") 
public class Movie { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "ID") 
    private String id; 

    @NotNull 
    @Column(name = "TITLE", nullable = false) 
    private String title; 
    @NotNull 
    @Column(name = "RATE", nullable = false) 
    private double rate; 
    @NotNull 
    @Column(name = "NUMBER_OF_RATES", nullable = false) 
    private int numberOfRates; 
    @Column(name = "CATEGORIES") 
    @Enumerated(EnumType.ORDINAL) 
    private Category categories; 
    @NotNull 
    @ManyToOne 
    private Director director; 
    @NotNull 
    @Column(name = "DURATION", nullable = false) 
    private int duration; 
    @NotNull 
    @Column(name = "PHOTO_URL", nullable = false) 
    private String photoUrl; 
    @Column(name = "DESCRIPTION") 
    private String description; 

//setters and getters 
} 

とサンプルはJSONを返しました:

{ 
    "id": "interstellar", 
    "title": "Interstellar", 
    "rate": 8.6, 
    "numberOfRates": 930496, 
    "categories": "ADVENTURE", 
    "director": { 
    "id": "christopher-nolan", 
    "name": "Christopher Nolan", 
    "location": "London, England, UK", 
    "movies": [ 
     { 
     "id": "inception", 
     "title": "Inception", 
     "rate": 8.8, 
     "numberOfRates": 1470268, 
     "categories": "ACTION", 
     "director": "christopher-nolan", 
     "duration": 123, 
     "photoUrl": "http://cdn.persiangig.com/preview/Ku3leEm3N7/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw%40%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO." 
     }, 
     { 
     "id": "interstellar", 
     "title": "Interstellar", 
     "rate": 8.6, 
     "numberOfRates": 930496, 
     "categories": "ADVENTURE", 
     "director": "christopher-nolan", 
     "duration": 169, 
     "photoUrl": "http://cdn.persiangig.com/preview/tSqxSLMKg5/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival." 
     }, 
     { 
     "id": "the-dark-knight", 
     "title": "The Dark Knight", 
     "rate": 9, 
     "numberOfRates": 1678434, 
     "categories": "ACTION", 
     "director": "christopher-nolan", 
     "duration": 152, 
     "photoUrl": "http://cdn.persiangig.com/preview/SyC1yqkQ55/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw%40%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice." 
     }, 
     { 
     "id": "the-dark-knight-rises", 
     "title": "The Dark Knight Rises", 
     "rate": 8.5, 
     "numberOfRates": 1146075, 
     "categories": "ACTION", 
     "director": "christopher-nolan", 
     "duration": 164, 
     "photoUrl": "http://cdn.persiangig.com/preview/UTo1oyUVDH/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw%40%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
     "description": "Eight years after the Joker's reign of anarchy, the Dark Knight, with the help of the enigmatic Selina, is forced from his imposed exile to save Gotham City, now on the edge of total annihilation, from the brutal guerrilla terrorist Bane." 
     } 
    ], 
    "positions": "DIRECTOR", 
    "photoUrl": "http://cdn.persiangig.com/preview/xNYollZv1Y/MV5BNjE3NDQyOTYyMV5BMl5BanBnXkFtZTcwODcyODU2Mw%40%40._V1_UY317_CR7%2C0%2C214%2C317_AL_.jpg" 
    }, 
    "duration": 169, 
    "photoUrl": "http://cdn.persiangig.com/preview/tSqxSLMKg5/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE%40._V1_UX182_CR0%2C0%2C182%2C268_AL_.jpg", 
    "description": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival." 
} 

が、私はこれをどのように修正することができますか?

答えて

1

映画のフェッチタイプをEagerからLazyに変更するだけです。

+0

「500内部サーバーエラー」 – Yashar

+0

例外は何ですか? – aviad

+0

'HTTPステータス500 - コンテンツを書き込めませんでした:ロールのコレクションを遅延して初期化できませんでした:ir.yasharne.imdb.model.Director.movi​​es、プロキシを初期化できませんでした - セッションはありません(参照チェーン:ir.yasharne.imdb。 model.Director ["movies"]); ' – Yashar

0

あなたがFetchType.EAGERを書いたからです。これをFetchType.LAZYに変更すると、オブジェクトが遅延して初期化され、必要になったときにフェッチし、Directorクラスのメソッドを呼び出すことで呼び出すことができます。

関連する問題