hibernate - 防止获取某些属性

时间:2016-08-20 05:46:14

标签: java json spring hibernate

我有一个Director类,其中包含Movies列表和一个包含director对象的Movie类。

问题在于,每当我获取一部电影时,它也会获取相应的导演,因此,它还会返回该导演指导的电影列表。

我的问题是,每当我拍摄电影时,如何防止拍摄电影列表?(当我单独取一个导演时,我仍想拍摄电影列表)

这是我的导演班:

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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

只需将电影的抓取类型从Eager更改为Lazy。

答案 1 :(得分:0)

那是因为你写了FetchType.EAGER。将其更改为FetchType.LAZY,以便它将延迟初始化该对象并仅在必要时获取并通过调用Director类方法调用它。