java - hibernate - 防止获取某些属性

标签 java json spring hibernate

我有一个包含电影列表的导演类和一个包含导演对象的电影类。

问题是每当我获取一部电影时,它也会获取相应的导演,结果,它还会返回该导演执导的电影列表。

我的问题是如何防止每次取电影时都取电影列表?(我仍然想在单独取导演时取电影列表)

这是我的 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."
}

我该如何解决这个问题?

最佳答案

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

关于java - hibernate - 防止获取某些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050897/

相关文章:

java - 如何将第一个图像从 ArrayList<string> 设置到 imageView 中

java - 将 JSON 解析为枚举字段

java - Spring 批处理中的多个读取器/处理器/写入器

json - 结果必须是一个数组,得到 : object in Zapier?

java - 如何在发布请求中使用 jersey(jax-rs) 客户端将一个对象作为另一个对象的属性传递?

java - ResourceBundle [messages] 未找到 MessageSource : Can't find bundle for base name messages

java - 每当调用任何父类(super class)的构造函数时,使子类执行某些操作

java - 存储要从另一个 Activity 调用的 Activity 列表的最佳方式

java - 无法启动 Swing 计时器

android - 巨大的 HttpPost 响应 : JSON