hibernate - предотвращает выбор некоторых свойств

java json spring hibernate

51 просмотра

2 ответа

У меня есть класс Director, который содержит список фильмов и класс Movie, который содержит объект 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"
}

и мой класс Кино:

@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."
}

Как я могу это исправить?

Автор: Yashar Источник Размещён: 08.11.2019 10:56

Ответы (2)


1 плюс

Просто измените тип загрузки фильмов с Eager на Lazy.

Автор: aviad Размещён: 20.08.2016 06:00

0 плюса

Это потому что ты написал FetchType.EAGER. Измените его на FetchType.LAZYтак, чтобы он лениво инициализировал этот объект и выбирал только тогда, когда это необходимо, и вы вызываете его, вызывая методы класса Director.

Автор: Sumit Badaya Размещён: 20.08.2016 06:26
Вопросы из категории :
32x32