Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate-防止提取某些属性_Java_Json_Spring_Hibernate - Fatal编程技术网

Java hibernate-防止提取某些属性

Java hibernate-防止提取某些属性,java,json,spring,hibernate,Java,Json,Spring,Hibernate,我有一个包含电影列表的Director类和一个包含Director对象的Movie类 问题是,每当我获取一部电影时,它也会获取相应的导演,因此,它也会返回该导演所导演的电影列表 我的问题是,我如何防止在每次拍摄电影时提取电影列表?(当我单独拍摄导演时,我仍然希望提取电影列表) 这是我的导演课: @Entity @Table(name = "DIRECTOR") @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.cla

我有一个包含电影列表的Director类和一个包含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
}
还有我的电影课:

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

如何修复此问题?

只需将电影的获取类型从“渴望”更改为“懒惰”。

这是因为您编写了
FetchType.Eager
。将其更改为
FetchType.LAZY
,这样它将延迟初始化该对象并仅在必要时提取,您可以通过调用Director类方法调用它。

有什么异常?
HTTP状态500-无法写入内容:未能延迟初始化角色集合:ir.yashanne.imdb.model.Director.movies,无法初始化代理-无会话(通过引用链:ir.yashanne.imdb.model.Director[“movies”])当您移出当前会话范围时,会发生此问题。会话完成其事务并在关闭会话后离开会话,新会话的作用域中没有延迟加载的对象,因此您无法初始化代理-无会话。要解决这个问题,请将代码移动到服务层上的一个事务中。之所以会发生这种情况,是因为json映射程序试图映射此集合,但它没有初始化。我不知道使用哪种json映射器,但需要对其进行配置,以便跳过未初始化的关联(通过在字段中选中Hibernate.isInitialized)